5

I`m writing a template to learn a little bit more about LaTeX, and I am not satisfied with my \chapter{} command. I tried to make a mini TOC with the chapter number like the figure below: enter image description here

However, I want some specific changes to make this one perfect to me.

  1. First, I get very uncomfortable with the different spacing on the right and left sides, and I don`t know how fix this, moreover, I want the spacing in all sides to be the same, not like the image below: enter image description here

  2. Second, I would really like the content inside the mini table of contents to adapt to the size of the rectangle that contains it, so that the more sections within it, the smaller the font will be to fit all the sections.

  3. Third, I get the warning Overfull \hbox (1.22324pt too wide) in paragraph at lines 15--15 when I type the command \chapter{Entering the commands} and I really have no idea why.

Well, my MWE is this:

My .cls file:

\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}
}

My .tex file:

\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}

Additional comments:

I remove the number of each section to keep the mini-TOC clean;

If this code doesn't work, it is possible that I have forgotten some information in the post, because as the .cls is very large, I didn't want to put everything so that it wouldn't be difficult to understand, but for me this code works;

I'm compiling with pdfLaTeX.

Alan Munn
  • 218,180
Jimeens
  • 617

2 Answers2

5

Here is my solution.

In the .cls file, I keep the xcolor package. It isn't needed as tikz provide access to colors, but so, we can use global option like divpsnames in the document class for accessing to more color names.

I have added marginparwidth = 1.25cm in the options of the geometry package (so there is an little margin).

I have kept your code for avoid new page at the beginning of a new chapter (\patchcmd{\chapter}{...}).

My code uses the TikZ library calc, but you have already loaded it in your class file, so I don't have to adding it. But remember to not remove it.

In the code that format the chapter title (see \titleformat), I have added \fontfamily{cmr} in order to have access to bold small caps.

In the setting of \titlespacing, I have used fixed lengths, but your are free to use rubbed lengths instead.

I have set the title class (a titlesec concept) to straight in order to keep vertical space before the chapter block, unless it is a the top of a page. With the showframe option of the geometry package, you can see in the third screenshot that there is no vertical space when a chapter begins at the top of the page. Note that if you don't have chosen to allow chapter begins in the middle of a page, setting the vertical space added before to 0pt in \titlespacing isn't suffisant. There's a little space remaining (see https://github.com/jbezos/titlesec/issues/53), and as the package author won't correct this "bug" as it is the same behaviour in the LaTeX kernel, there is a workaround here: Package titlesec adds extra space on top of \chapter, despite commands to the contrary.

So, if you will not have chapter begins in the middle of the page, after removing the code \patchcmd{\chapter}{...}, you have to set (in \titlespacing) the vertical space before to 0pt AND add this little code:

\makeatletter % the macro name contains @
\patchcmd{\ttl@mkchap@i}{\vspace*{\@tempskipa}}{}{}{}
\makeatother

In the rest of the code, I set some lengths (80% of the \textwidth for the minitoc block, 15% of the \textwidth for the chapter number block, the inner sep of the minitoc block, the column separation.

If chapter number goes further than 9, you need to adjust (reduce) the size of the displayed number (in the second \node in the code of the \chapterNumberAndMinitoc command).

Then I set the formatting of the partial Table of Content.

Then, we are coming to the \chapterNumberAndMinitoc code.

We have 4 steps: displaying the minitoc block at left, displaying the background of the chapter number at the right, with the same hight of the minitoc bloc, the display the chapter number at is center, and finally display the rotated text at the center of the space between the two colored blocks.

I have removed your code \renewcommand{\section}[1]{...}.

Note that you have used a very dark color (named myblack) for the minitoc bloc, the chapter title, the chapter number, the chapter rules, but not for the rotated text. I have kept the black for the rotated text.

In order to demonstrate the usage of this LaTeX class in real situation, I have used a lot of fake text with the lipsum package (that you have already loaded in you original class file). You can adjust some spaces for the partial TOC if you need to display page numbers above 99 (see at \titlecontents{p-section}, and replace {} by {\contentsmargin{20pt}} in the code before, so you have space for a third digit for the page number).

The solutionclass.cls file:

\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}% }

the test.tex file:

\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}

Some screenshots of the output (using the showframe document class option, provided by the geometry package):

enter image description here

enter image description here

enter image description here

quark67
  • 4,166
3

Partial response

the different spacing on the right and left sides,

Overfull \hbox (1.22324pt too wide)

I added

\titlecontents{section}
[0em]
{}
{}
{}
{\titlerule*[1pc]{.}\contentspage}

I also added the inner sep and outer sep to calculate the available width for the last node with the xfp package

\fpeval{\textwidth-2.5cm-20pt-5mm}pt
  • 2.5cm the width of the first node
  • 20pt the height (rotate 90) of the 2nd node
  • 5mm 2 x the inner sep of the 3rd node

Added the % behind the end minipage

EDIT

I would really like the content inside the mini table of contents to adapt to the size of the rectangle that contains it

I use the fitting library of 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}

enter image description here

pascal974
  • 4,652
  • It worked very well, the only thing missing was the font size to be adaptable to the amount of sections, but thank you very much. – Jimeens Mar 13 '23 at 14:44
  • 1
    With the fitting library of tcolorbox, we can adapt the fontsize, I modified my answer – pascal974 Mar 15 '23 at 05:38