使用 titlesec 为章节创建特定样式

使用 titlesec 为章节创建特定样式

我正在编写一个模板来进一步了解 LaTeX,但我对自己的\chapter{}命令不满意。我尝试制作一个带有章节编号的迷你目录,如下图所示: 在此处输入图片描述

然而,我希望进行一些特定的改变,使这个对我来说变得完美。

  1. 首先,我对左右两边的不同间距感到非常不舒服,我不知道如何解决这个问题,而且,我希望所有边的间距都相同,而不是像下面的图片这样: 在此处输入图片描述

  2. 其次,我真的希望迷你目录内的内容能够适应包含它的矩形的大小,这样其中的部分越多,字体就越小,以适合所有部分。

  3. 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}

补充评论:

我删除了每个部分的编号以保持 mini-TOC 的清洁;

如果此代码不起作用,则可能是我忘记了帖子中的某些信息,因为由于内容.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}{...})。

我的代码使用 TiZ 库calc,但您已经在类文件中加载了它,因此我不必添加它。但请记住不要删除它。

在格式化章节标题的代码中(参见\titleformat),我添加了\fontfamily{cmr}以便能够访问粗体小型大写字母。

在的设置中\titlespacing,我使用了固定长度,但您可以自由使用摩擦长度。

我已经设定了标题班级(A标题安全概念)以straight在章节块之前保留垂直空间,除非它是页面的顶部。使用showframe选项几何学包中,您可以在第三张屏幕截图中看到,当章节从页面顶部开始时,没有垂直空间。请注意,如果您没有选择允许章节从页面中间开始,则将之前添加的垂直空间设置为0ptin\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)作为米尼托克bloc、章节标题、章节编号、章节规则,但不包括旋转文本。我保留了旋转文本的黑色。

为了演示此 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几何学包裹):

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

相关内容