我正在編寫一個模板來更多地了解 LaTeX,但我對我的\chapter{}
命令並不滿意。我嘗試製作一個帶有章節號碼的迷你目錄,如下圖:
然而,我想要一些具體的改變,使這對我來說更完美。
首先,我對左右兩側不同的間距感到非常不舒服,而且我不知道如何解決這個問題,而且,我希望所有邊的間距都相同,而不是像下圖這樣:
其次,我真的希望迷你目錄中的內容能夠適應包含它的矩形的大小,這樣其中的部分越多,字體就越小,以適合所有部分。
Overfull \hbox (1.22324pt too wide) in paragraph at lines 15--15
第三,當我輸入命令時,我收到警告\chapter{Entering the commands}
,但我真的不知道為什麼。
嗯,我的 MWE 是這樣的:
我的.cls
文件:
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{solutionclass}[2023/03/03 My Custom LaTeX Class for exercise solutions]
\LoadClass[a4paper, twoside, 11pt]{book}
% LANGUAGE AND ENCODING
\RequirePackage[portuguese, english]{babel}
\RequirePackage[utf8]{inputenc}
\RequirePackage[T1]{fontenc}
% TEXT FORMATTING AND MANIPULATION
\RequirePackage{csquotes}
\RequirePackage{mathrsfs}
\RequirePackage{titlesec}
\RequirePackage{lipsum}
% FIGURES, TIKZ AND BOXES
\RequirePackage{graphicx}
\RequirePackage{float}
\RequirePackage{xcolor}
\RequirePackage{tikz}
\RequirePackage{tikz-3dplot}
\RequirePackage{pgfplots}
\RequirePackage[most]{tcolorbox}
% PAGE AND TEXT STYLES
\RequirePackage[colorlinks = true,
urlcolor = LinkColor,
anchorcolor = LinkColor,
citecolor = LinkColor,
filecolor = LinkColor,
linkcolor = LinkColor,
menucolor = LinkColor,
linktocpage = true,
bookmarks = true,
pdfusetitle]{hyperref}
\RequirePackage[top = 2.5cm,
bottom = 3.8cm,
left = 2cm,
right = 2cm,
]{geometry}
\RequirePackage{microtype}
\RequirePackage{titletoc}
\RequirePackage{multicol}
\RequirePackage{lmodern}
\RequirePackage{nameref}
\usetikzlibrary{%
decorations.pathreplacing,
decorations.pathmorphing,
decorations.markings,
shapes.multipart,
shapes.geometric,
arrows.meta,
tikzmark,
fadings,
arrows,
angles,
quotes,
calc,
3d,
}
\pgfplotsset{compat=1.18}
% COLOR SETTINGS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\definecolor{myblack}{HTML}{101010} % PERSONAL BLACK
\definecolor{myorangeI}{HTML}{e93820} % PERSONAL ORANGE I
\definecolor{LinkColor}{HTML}{eb5a00} % PERSONAL ORANGE II
% CHAPTER SETTINGS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\chapterNumberAndMinitoc}{%
\begin{tikzpicture}
\node[%
minimum height = 3.2cm,
text width = 2.5cm,
outer sep = 0pt,
align = center,
fill = myblack,
font = \color{white}\fontsize{80}{90}\selectfont,
] (num) {\thechapter};
\node[%
rotate = 90,
anchor = south,
font = \color{black}\Large\normalfont
] at ([xshift=-3pt] num.west) {\textls[180]{\textsc{List N°}}};
\node[%
minimum height = 3.2cm,% Height of the box
minimum width = 6.2cm,
inner sep = 2.5mm, % The border thickness
align = left, % Text aligning
fill = myblack, % Color background
] at ([xshift=-10.3cm] num.east) {%
\color{white}
\begin{minipage}{0.7552\textwidth} % Control the width of the box
\begin{multicols}{2}
\printcontents[sections]{}{1}{}{}
\end{multicols}
\end{minipage}
};
\end{tikzpicture}%
}
\titleformat{\chapter}[display]
{\normalfont\bfseries\color{myblack}}
{\filleft%
\chapterNumberAndMinitoc}{1em}
{\titlerule[2.5pt]\vspace{3pt}\titlerule\vspace{4pt}\LARGE\textsc}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\patchcmd{\chapter}{%
\if@openright
\cleardoublepage
\else
\clearpage
\fi
}{%
}{}{}
\makeatother
\let\oldsection\section
\renewcommand{\section}[1]{%
\oldsection*{#1}
\phantomsection
\addcontentsline{toc}{section}{#1}
}
我的.tex
文件:
\documentclass[]{solutionclass}
\pagestyle{plain}
\begin{document}
\makeatletter
\startcontents[sections]
\phantomsection
\chapter{Entering the commands} % Here I have the warning
\makeatother
\section{Unbreakable Form}
\section{Breakable Form}
\section{Using the references}
\section{Equations style}
\section{Additional Boxes}
\section{Additional Commands}
\end{document}
補充評論:
我刪除了每個部分的編號以保持迷你目錄的乾淨;
如果這段程式碼不起作用,可能是我忘記了帖子中的一些信息,因為由於程式碼
.cls
非常大,我不想把所有內容都寫出來,這樣就不難理解了,但對我來說這段程式碼有效;
我正在使用 pdfLaTeX 進行編譯。
答案1
部分回應
左右兩側的間距不同,
\hbox 過滿(1.22324pt 太寬)
我添加了
\titlecontents{section}
[0em]
{}
{}
{}
{\titlerule*[1pc]{.}\contentspage}
我還添加了inner sep
和來計算包outer sep
中最後一個節點的可用寬度xfp
\fpeval{\textwidth-2.5cm-20pt-5mm}pt
- 第一個節點寬度2.5cm
- 20pt 第二個節點的高度(旋轉 90)
- 5mm 2 x 第三個節點的內間隔
在結束小頁面後面添加了 %
編輯
我真的希望迷你目錄中的內容能夠適應包含它的矩形的大小
我使用的fitting
庫tcolorbox
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{solutionclass}[2023/03/03 My Custom LaTeX Class for exercise solutions]
\LoadClass[a4paper, twoside, 11pt]{book}
% LANGUAGE AND ENCODING
\RequirePackage[portuguese, english]{babel}
\RequirePackage[utf8]{inputenc}
\RequirePackage[T1]{fontenc}
% TEXT FORMATTING AND MANIPULATION
\RequirePackage{csquotes}
\RequirePackage{mathrsfs}
\RequirePackage{titlesec}
\RequirePackage{lipsum}
% FIGURES, TIKZ AND BOXES
\RequirePackage{graphicx}
\RequirePackage{float}
\RequirePackage{xcolor}
\RequirePackage{tikz}
\RequirePackage{tikz-3dplot}
\RequirePackage{pgfplots}
\RequirePackage[most]{tcolorbox}
% PAGE AND TEXT STYLES
\RequirePackage[colorlinks = true,
urlcolor = LinkColor,
anchorcolor = LinkColor,
citecolor = LinkColor,
filecolor = LinkColor,
linkcolor = LinkColor,
menucolor = LinkColor,
linktocpage = true,
bookmarks = true,
pdfusetitle]{hyperref}
\RequirePackage[top = 2.5cm,
bottom = 3.8cm,
left = 2cm,
right = 2cm,
]{geometry}
\RequirePackage{microtype}
\RequirePackage{titletoc}
\RequirePackage{multicol}
\RequirePackage{lmodern}
\RequirePackage{nameref}
\RequirePackage{xfp}%<--- added
\usetikzlibrary{%
decorations.pathreplacing,
decorations.pathmorphing,
decorations.markings,
shapes.multipart,
shapes.geometric,
arrows.meta,
tikzmark,
fadings,
arrows,
angles,
quotes,
calc,
3d,
}
\pgfplotsset{compat=1.18}
% COLOR SETTINGS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\definecolor{myblack}{HTML}{101010} % PERSONAL BLACK
\definecolor{myorangeI}{HTML}{e93820} % PERSONAL ORANGE I
\definecolor{LinkColor}{HTML}{eb5a00} % PERSONAL ORANGE II
% CHAPTER SETTINGS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\chapterNumberAndMinitoc}{%
\begin{tikzpicture}
\node[%
minimum height = 3.2cm,
text width = 2.5cm,
outer sep = 0pt,
inner sep = 0pt,%<--- added
align = center,
fill = myblack,
font = \color{white}\fontsize{80}{90}\selectfont,
] (num) {\thechapter};
\node[%
minimum height = 20pt,%<--- added
outer sep = 0pt,%<--- added
inner sep = 0pt,%<--- added
rotate = 90,
anchor = south,
font = \color{black}\Large\normalfont
]
(listN)%<--- added
at
(num.west)
{\textls[180]{\textsc{List N°}}};
\node[%
inner sep = 0pt, % The border thickness
outer sep = 0pt,%<--- added
anchor = east,
] at
(listN.north) {%<--- added
%\color{white}
% \begin{minipage}{\fpeval{\textwidth-2.5cm-20pt-5mm}pt} % Control the width of the box
% %\setlength{\columnsep}{0pt}
% \begin{multicols}{2}
% \printcontents[sections]{}{1}{}{}
% \end{multicols}
% \end{minipage}%<--- added DON't FORGET
%%%%%%%%%%%%%% EDIT
\begin{tcolorbox}[
width=\fpeval{\textwidth-2.5cm-20pt}pt,
enhanced,
boxrule=0pt,
sharp corners,
left=2.5mm,
right=2.5mm,
top=2.5mm,
bottom=2.5mm,
valign=center,
colback=myblack,
fit to height=3.2cm,
fit fontsize macros
]
\color{white}
\begin{multicols}{2}
\printcontents[sections]{}{1}{}{}
\end{multicols}
\end{tcolorbox}
};
\end{tikzpicture}%
}
\titleformat{\chapter}[display]
{\normalfont\bfseries\color{myblack}}
{\filleft%
\chapterNumberAndMinitoc}{1em}
{\titlerule[2.5pt]\vspace{3pt}\titlerule\vspace{4pt}\LARGE\textsc}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\patchcmd{\chapter}{%
\if@openright
\cleardoublepage
\else
\clearpage
\fi
}{%
}{}{}
\makeatother
\let\oldsection\section
\renewcommand{\section}[1]{%
\oldsection*{#1}
\phantomsection
\addcontentsline{toc}{section}{#1}
}
%%%%%%%%%%% Added
\titlecontents{section}
[0em]
{\small}
{}
{}
{\titlerule*[1pc]{.}\contentspage}
答案2
這是我的解決方案。
在 .cls 檔案中,我保留了該xcolor
套件。不需要它,因為蒂克茲提供對顏色的訪問,但是我們可以使用divpsnames
文檔類中的全域選項來訪問更多顏色名稱。
我已經marginparwidth = 1.25cm
在選項中新增了幾何學封裝(所以有一點餘裕)。
我保留了您的程式碼,以避免在新章節的開頭出現新頁面(\patchcmd{\chapter}{...}
)。
我的程式碼使用 TikZ 庫calc
,但您已經將其載入到類別文件中,因此我不必添加它。但記住不要刪除它。
在格式化章節標題的程式碼中(請參閱 參考資料\titleformat
),我添加了一些內容,\fontfamily{cmr}
以便能夠存取粗體小型大寫字母。
在 的設定中\titlespacing
,我使用了固定長度,但您可以隨意使用摩擦長度。
我已經設定了標題班級(A標題安全概念),以便straight
在章節塊之前保持垂直空間,除非它是頁面的頂部。與showframe
選項幾何學包中,您可以在第三個螢幕截圖中看到,當章節從頁面頂部開始時,沒有垂直空間。請注意,如果您沒有選擇允許章節從頁面中間開始,則將先前新增的垂直空間設為0pt
in\titlespacing
是不夠的。還剩下一點空間(請參閱https://github.com/jbezos/titlesec/issues/53),並且由於套件作者不會糾正這個“錯誤”,因為它與 LaTeX 核心中的行為相同,因此這裡有一個解決方法:套件 titlesec 在 \chapter 頂部添加了額外的空間,儘管命令相反。
因此,如果您不希望章節從頁面中間開始,則在刪除程式碼後\patchcmd{\chapter}{...}
,您必須設定(在\titlespacing
)之前的垂直空間加入0pt
這個小程式碼:
\makeatletter % the macro name contains @
\patchcmd{\ttl@mkchap@i}{\vspace*{\@tempskipa}}{}{}{}
\makeatother
在程式碼的其餘部分中,我設定了一些長度(80% 的長度\textwidth
用於迷你托克\textwidth
塊,15%的章節號塊,內九月的迷你托克塊、柱分離。
如果章節編號超過 9,則需要調整(減少)顯示編號的大小(在\node
命令代碼的第二個中\chapterNumberAndMinitoc
)。
然後我設定部分目錄的格式。
然後,我們來看看\chapterNumberAndMinitoc
程式碼。
我們有 4 個步驟:顯示迷你托克左側區塊,顯示右側章節號的背景,高度與右側區塊相同迷你托克bloc,以中心顯示章節號,最後在兩個彩色區塊之間的空間中心顯示旋轉後的文字。
我已經刪除了你的程式碼\renewcommand{\section}[1]{...}
。
請注意,您使用了非常暗的顏色(名為myblack
)作為迷你托克區塊、章節標題、章節編號、章節規則,但不適用於旋轉文字。我保留了旋轉文字的黑色。
為了示範這個 LaTeX 類別在實際情況中的用法,我使用了很多假文本利蘇姆包(您已經在原始類別文件中載入)。如果需要顯示 99 以上的頁碼,可以調整部分目錄的一些空格(參見,\titlecontents{p-section}
並{}
在{\contentsmargin{20pt}}
之前的程式碼,因此您有空間容納頁碼的第三位數字)。
文件solutionclass.cls
:
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{solutionclass}[2023/03/15 My Custom LaTeX Class for exercise solutions]
\LoadClass[a4paper, twoside, 11pt]{book}
% LANGUAGE AND ENCODING
\RequirePackage[portuguese, english]{babel}
\RequirePackage[utf8]{inputenc}
\RequirePackage[T1]{fontenc}
% TEXT FORMATTING AND MANIPULATION
\RequirePackage{csquotes}
\RequirePackage{mathrsfs}
\RequirePackage{titlesec}
\RequirePackage{lipsum}
% FIGURES, TIKZ AND BOXES
\RequirePackage{graphicx}
\RequirePackage{float}
\RequirePackage{xcolor}
\RequirePackage{tikz}
\RequirePackage{tikz-3dplot}
\RequirePackage{pgfplots}
\RequirePackage[most]{tcolorbox}
% PAGE AND TEXT STYLES
\RequirePackage[colorlinks = true,
urlcolor = LinkColor,
anchorcolor = LinkColor,
citecolor = LinkColor,
filecolor = LinkColor,
linkcolor = LinkColor,
menucolor = LinkColor,
linktocpage = true,
bookmarks = true,
pdfusetitle]{hyperref}
\RequirePackage[top = 2.5cm,
bottom = 3.8cm,
left = 2cm,
right = 2cm,
marginparwidth = 1.25cm % <- added
]{geometry}
\RequirePackage{microtype}
\RequirePackage{titletoc}
\RequirePackage{multicol}
\RequirePackage{lmodern}
\RequirePackage{nameref}
\usetikzlibrary{%
decorations.pathreplacing,
decorations.pathmorphing,
decorations.markings,
shapes.multipart,
shapes.geometric,
arrows.meta,
tikzmark,
fadings,
arrows,
angles,
quotes,
calc,
3d,
}
\pgfplotsset{compat=1.18}
% COLOR SETTINGS
%%%%%%%%%%%%%%%%%%%%
\definecolor{myblack}{HTML}{101010} % PERSONAL BLACK
\definecolor{myorangeI}{HTML}{e93820} % PERSONAL ORANGE I
\definecolor{LinkColor}{HTML}{eb5a00} % PERSONAL ORANGE II
% CHAPTER SETTINGS
%%%%%%%%%%%%%%%%%%%%
% Avoid new page at the beginning of a new chapter
\makeatletter
\patchcmd{\chapter}{%
\if@openright
\cleardoublepage
\else
\clearpage
\fi
}{%
}{}{}
\makeatother
% formatting of the chapter title (number+text)
\titleformat{\chapter}% sectionning type to customize
[display]% shape, here number and title text on separate lines
{\normalfont\bfseries\color{myblack}\startcontents[sections]}%format; begins a partial toc named "sections" at each new chapter
{\chapterNumberAndMinitoc}% formatting code of the section (here chapter) number. Instead only number, this code also displays a partial TOC (sections in the current chapter)
{1em}% in "display" shape, the distance between the "number" and the "title", here he distance between the minitoc block and the rule above the chapter title
{\titlerule[2.5pt]\vspace{3pt}\titlerule\vspace{4pt}\LARGE\fontfamily{cmr}\textsc}% code before the title text. Here for rules and the title format. Added \fontfamily{cmr} for the bold small caps
% Setting of the space before and after the title
\titlespacing{\chapter}% Place the chapter block almost at top of the page
{0pt}% space added at left
{20pt}% vertical space added before
{20pt}% vertical space added after (space between chapter title and section title, for example)
\titleclass{\chapter}{straight} % so, if the chapter is at the beginning of a page, there is no vertical space before
% Further geometry settings
\columnsep=20pt % gap between the two columns in the multicols environment
\newlength{\minitocInnerSep}% inner sep in the minitoc node
\setlength{\minitocInnerSep}{10pt}
\newlength{\minitocWidth}% width of the minitoc block
\setlength{\minitocWidth}{0.8\textwidth}
\newlength{\chapterBlockWidth}% width of the block with the chapter number
\setlength{\chapterBlockWidth}{0.15\textwidth} % this value must be less than (1-0.8)\textwidth; here with 1-0.8-0.15=0.05, we have 0.05 for the width of the region of the rotated text. Never be less than 0.03.
\newlength{\minipageWidth}% width of the minipage in the minitoc
\setlength{\minipageWidth}{\dimexpr\minitocWidth-2\minitocInnerSep}%So the width of the minitoc block remains \minitocWidth
% formatting the section type (named p-section, see the prefix in \printcontents[sections] below) in the partial toc
\titlecontents{p-section}%
[0pt]% left margin, i.e. space before the text, generally for the section number
{}% code before (global formatting code).
{}% section number formatting code
{}% formatting code for sections without number
{\titlerule*[6pt]{.}\contentspage}% filler (here, dotted line) formatting. Followed by the page number (aka \contentspage)
\newcommand{\chapterNumberAndMinitoc}{%
\begin{tikzpicture}
% minitoc node
\node[%
minimum height = 3.2 cm,% Minimum height of the box
inner sep = \minitocInnerSep,% The border thickness
fill = myblack,% Color background
outer sep = 0pt %Needed to avoid overfull hbox
] (minitoc) {%
\color{white}
\begin{minipage}{\minipageWidth}
\begin{multicols}{2}
%displays the partial toc
\printcontents[sections]%
{p-}% prefix; so we can adjust the layout of the partial toc without changing the main (here name = p-section for modifying the sections in this partial toc)
{1}% display the partial toc from level 1 (0=chapter, 1=section)
[1]% ... to the level 1 (section)
{}% code for the partial toc
\end{multicols}
\end{minipage}
};
%Draw of the chapter block
\coordinate (chapterBlockNE) at ($(minitoc.north west)+(\textwidth,0)$);
\coordinate (chapterBlockSW) at ($(minitoc.south west)+(\textwidth-\chapterBlockWidth,0)$);
\fill[myblack] (chapterBlockNE) rectangle (chapterBlockSW);
%Display the chapter number
\node[%
anchor = center,
font = \color{white}\fontsize{80}{90}\selectfont %
] at ($(chapterBlockNE)!0.5!(chapterBlockSW)$) {\thechapter};
%Display the rotated text
\coordinate (rotatedTextC) at ($(minitoc.north east)!0.5!(chapterBlockSW)$);% center of the rotated text
\node[%
rotate = 90,
anchor = center,
font = \color{black}\Large\normalfont
] at (rotatedTextC) {\textls[180]{\textsc{List} N\textsuperscript{o}}};
\end{tikzpicture}%
}
文件test.tex
:
\documentclass[showframe]{solutionclass}
\pagestyle{plain}
\begin{document}
\tableofcontents
\chapter{Entering the commands}
\section{Unbreakable Form}
\subsection{Subsection}
\section{Breakable Form}
\lipsum[1-70]
\section{Using the references}
\section{Equations style}
\lipsum[1-40]
\section{Additional Boxes}
\lipsum[1-34]
\section{Additional Commands}
\section{Equations style}
\section{Additional Boxes}
\lipsum[1-7]
\section{Additional Commands}
\section{Additional Boxes}
\section{Additional Commands}
\lipsum[1-6]
\chapter{New Chapter}
\section{New Section}
text
\newpage
\chapter{Another New Chapter}
\section{A New Section}
\end{document}
輸出的一些螢幕截圖(使用showframe
文檔類別選項,由幾何學包裹):