LaTeX
Thứ Hai, 25/07/2016, 00:07
Tùy chỉnh cách đánh số chapter, section, subsection trong LaTeX

Tùy chỉnh cách đánh số chapter, section, subsection trong LaTeX

Mặc định LaTeX sẽ đánh số các section, subsection theo dạng 1.1, 1.1.1, 2.1 hay đại loại thế. Bây giờ bạn muốn tùy chỉnh cách đánh số này như chuyển về số La Mã, chỉ đánh 1, 2, 3 thôi thì phải làm sao? Bài viết này sẽ cố gắng giúp bạn làm điều này. Ý tưởng đến từ một câu hỏi của bạn Trần Văn Hoàng trên trang facebook của Math2IT.
[mks_icon icon="fa-chevron-right" color="#dd3333" type="fa"] Xem thêm : Hiện chữ "Chương" và "Phần" ở Mục Lục trong bài viết LaTeX.
Để cho dễ hiểu, ta có một đoạn code rất bình thường như sau
\documentclass{article} \usepackage[utf8]{vietnam} \begin{document} \tableofcontents \section{section a} \subsection{subsec a} \subsection{subsec b} \section{section b} \subsection{subsec a} \subsection{subsec b} \end{document}
Theo mặc định, nó sẽ cho ra kết quả giống như vầy (tôi chỉ chú trọng vào mục lục để dễ thấy cách nó đánh số thôi)
notion image
 
Ví dụ bạn muốn đổi giống như hình dưới đây
notion image
Cách làm rất đơn giản, cụ thể để ra được cái như ở trên, bạn chỉ cần thêm 2 dòng code sau vào trước \begin{document} là được
\renewcommand\thesection{\Roman{section}} \renewcommand\thesubsection{\arabic{subsection}}
 
Nếu bạn nào "lười", có thể copy toàn bộ đoạn code (đã chứa hai dòng code trên) ở dưới
\documentclass{article} \usepackage[utf8]{vietnam} \renewcommand\thesection{\Roman{section}} \renewcommand\thesubsection{\arabic{subsection}} \begin{document} \tableofcontents \section{section a} \subsection{subsec a} \subsection{subsec b} \section{section b} \subsection{subsec a} \subsection{subsec b} \end{document}
 
Bây giờ chúng ta sẽ "chơi" với các tùy chỉnh khác, xuất phát từ ý tưởng của hai dòng code này.
Bạn có thể định nghĩa lại các section, subsection, chapter, part,... bằng cách dùng \renewcommand\thesection , khi ấy thay \thesection  (section) bởi các cái khác mà bạn muốn. Lưu ý là "Phần" và "Chương" chỉ khả dụng với loại văn bản "book" và "report" thôi nha.
  • Phần (part) : \thepart
  • Chương (chapter) : \thechapter
  • Mục (section) : \thesection
  • Phụ mục (subsection) : \thesubsection
  • v.v...
Bây giờ tới lượt cách mà bạn muốn nó hiển thị, ở trên là chữ số La Mã, vậy còn cái gì khác?
  • \arabic (1, 2, 3, ...)
  • \alph (a, b, c, ...)
  • \Alph (A, B, C, ...)
  • \roman  (i, ii, iii, ...)
  • \Roman  (I, II, III, ...)
  • \fnsymbol  (∗, †, ‡, §, ¶, ...)
Đừng quên đối chiếu với hai dòng code ở phía trên để biết cách sử dụng nhé. Dưới đây là một ví dụ cho bạn dễ hiểu
notion image
\documentclass{book} \usepackage[utf8]{vietnam} \renewcommand\thepart{\Alph{part}} \renewcommand\thechapter{\Roman{chapter}} \renewcommand\thesection{\arabic{section}} \renewcommand\thesubsection{\thesection.\alph{subsection}/} \begin{document} \tableofcontents \part{Phần A} \chapter{Chương a} \section{section a} \subsection{subsec a} \subsection{subsec b} \section{section b} \subsection{subsec a} \subsection{subsec b} \part{Phần B} \end{document}
 

Vấn đề về màu sắc trong mục lục

Thông thường, trong tài liệu latex chúng ta thường dùng gói hyperref với option colorlinks=true để highlight các liên kết trong tài liệu như Mục lục, phương trình, hình ảnh, bảng biểu
\usepackage{hyperref} \hypersetup{ colorlinks=true }
Kết quả hiển thị ở phần Mục lục sẽ như hình sau
notion image
Tuy nhiên ta thấy, theo mặc định chỉ có tên của các mục được tô màu, còn các dấu chấm và số trang vẫn còn màu đen.
Để tô màu các dấu chấm và số trang phía sau, bạn cần sử dụng thêm gói tocloft và định nghĩa lại các biến \cftxxxfont, \cftxxxleader\cftxxxpagefont trong đó xxx tương ứng với các đơn vị cần định nghĩa lại (part,chap,sec,subsec, subsubsec,...)
\usepackage[dvipsnames]{xcolor} \usepackage{tocloft} \newcommand{\Color}[1]{\hypersetup{linkcolor=#1}\color{#1}} %Đoạn này định nghĩa màu cho tên Phần/Chương/Section/Subsection \renewcommand{\cftpartfont}{\Color{magenta}} \renewcommand{\cftchapfont}{\Color{blue}} \renewcommand{\cftsecfont}{\Color{LimeGreen}} \renewcommand{\cftsubsecfont}{\Color{red}} %Đoạn này định nghĩa màu cho dấu chấm phía sau Phần/Chương/Section/Subsection \renewcommand{\cftpartleader}{\Color{magenta}\cftdotfill{\cftsubsecdotsep}} \renewcommand{\cftchapleader}{\Color{blue}\cftdotfill{\cftsecdotsep}} \renewcommand{\cftsecleader}{\Color{LimeGreen}\cftdotfill{\cftsubsecdotsep}} \renewcommand{\cftsubsecleader}{\Color{red}\cftdotfill{\cftsecdotsep}} %Đoạn này định nghĩa màu cho số trang của Phần/Chương/Section/Subsection \renewcommand{\cftpartpagefont}{\Color{magenta}} \renewcommand{\cftchappagefont}{\Color{blue}} \renewcommand{\cftsecpagefont}{\Color{LimeGreen}} \renewcommand{\cftsubsecpagefont}{\Color{red}}
Kết quả sẽ như hình dưới đây
notion image
 
Và đây là đoạn code đầy đủ cho ví dụ trên, bạn bấm vào dấu cộng để hiện thị nó
\documentclass{book} \usepackage[utf8]{vietnam} \renewcommand\thepart{\Alph{part}} \renewcommand\thechapter{\Roman{chapter}} \renewcommand\thesection{\arabic{section}} \renewcommand\thesubsection{\thesection.\alph{subsection}/} \usepackage{hyperref} \hypersetup{ colorlinks=true, } \usepackage[dvipsnames]{xcolor} \usepackage{tocloft} \newcommand{\Color}[1]{\hypersetup{linkcolor=#1}\color{#1}} %Đoạn này định nghĩa màu cho tên Phần/Chương/Section/Subsection \renewcommand{\cftpartfont}{\Color{magenta}} \renewcommand{\cftchapfont}{\Color{blue}} \renewcommand{\cftsecfont}{\Color{LimeGreen}} \renewcommand{\cftsubsecfont}{\Color{red}} %Đoạn này định nghĩa màu cho dấu chấm phía sau Phần/Chương/Section/Subsection \renewcommand{\cftpartleader}{\Color{magenta}\cftdotfill{\cftsubsecdotsep}} \renewcommand{\cftchapleader}{\Color{blue}\cftdotfill{\cftsecdotsep}} \renewcommand{\cftsecleader}{\Color{LimeGreen}\cftdotfill{\cftsubsecdotsep}} \renewcommand{\cftsubsecleader}{\Color{red}\cftdotfill{\cftsecdotsep}} %Đoạn này định nghĩa màu cho số trang của Phần/Chương/Section/Subsection \renewcommand{\cftpartpagefont}{\Color{magenta}} \renewcommand{\cftchappagefont}{\Color{blue}} \renewcommand{\cftsecpagefont}{\Color{LimeGreen}} \renewcommand{\cftsubsecpagefont}{\Color{red}} \begin{document} \tableofcontents \part{Phần A} \chapter{Chương a} \section{section a} \subsection{subsec a} \section{section b} \subsection{subsec a} \part{Phần B} \end{document}
 
Nếu có thắc mắc gì thì đừng ngại comment bên dưới bài viết nhé. Chúc bạn thành công.
Cùng chủ đề