TeX - LaTeX Asked on February 28, 2021
I need to represent deeply nested container structures in technical documents.
In my representation, each container needs to have its own section with sub-containers being included in sub-sections.
In order to achieve this, I have defined custom sectioning commands past subparagraph using titlesec’s titleclass
command. My company’s .sty file defines the leaders in the table of contents to be solid lines using:
renewcommand{cftdot}{rule{1pt}{0.4pt}}
renewcommand{cftdotsep}{0}
According to the tocloft package documentation this should result in @dottedtocline
producing solid lines instead of dotted ones.
While this works for the default sectioning commands, my custom commands still produce dotted lines in the table of contents:
documentclass[a4paper,11pt]{report}
usepackage{titlesec}
usepackage[utf8]{inputenc}
usepackage[T1]{fontenc}
usepackage[titles]{tocloft}
%% use solid lines instead of dotted lines for toc
renewcommand{cftdot}{rule{1pt}{0.4pt}}
renewcommand{cftdotsep}{0}
%% Indentation
cftsetindents{section}{2em}{3em}
cftsetindents{subsection}{2em}{4.0em}
cftsetindents{subsubsection}{2em}{5.0em}
cftsetindents{paragraph}{2em}{6.0em}
cftsetindents{subparagraph}{2em}{7.0em}
titleformat{section}{largebf}{thesection}{1em}{}
titleformat{subsection}{bf}{thesubsection}{1em}{}
titleformat{subsubsection}{bf}{thesubsubsection}{1em}{}
titleformat{paragraph}{bf}{theparagraph}{1em}{}
titleformat{subparagraph}{bf}{thesubparagraph}{1em}{}
titlespacing{section}{0pt}{21pt}{3pt}
titlespacing{subsection}{0pt}{21pt}{3pt}
titlespacing{subsubsection}{0pt}{21pt}{3pt}
titlespacing{paragraph}{0pt}{21pt}{3pt}
titlespacing{subparagraph}{0pt}{21pt}{3pt}
newcommand{levelOneSubsection}[1]{subsection{#1}}
newcommand{levelTwoSubsection}[1]{subsubsection{#1}}
newcommand{levelThreeSubsection}[1]{paragraph{#1}}
newcommand{levelFourSubsection}[1]{subparagraph{#1}}
% define levelFiveSubsection
titleclass{levelFiveSubsection}{straight}[subparagraph]
newcounter{levelFiveSubsection}[subparagraph]
renewcommand{thelevelFiveSubsection}{thesubparagraph.arabic{levelFiveSubsection}}
titleformat{levelFiveSubsection}{bf}{thelevelFiveSubsection}{1em}{}
titlespacing{levelFiveSubsection}{0pt}{21pt}{3pt}
makeatletter
deftoclevel@levelFiveSubsection{5}
defl@levelFiveSubsection{@dottedtocline{5}{2em}{8em}}
makeatother
% define levelSixSubsection
titleclass{levelSixSubsection}{straight}[levelFiveSubsection]
newcounter{levelSixSubsection}[levelFiveSubsection]
renewcommand{thelevelSixSubsection}{thelevelFiveSubsection.arabic{levelSixSubsection}}
titleformat{levelSixSubsection}{bf}{thelevelSixSubsection}{1em}{}
titlespacing{levelSixSubsection}{0pt}{21pt}{3pt}
makeatletter
deftoclevel@levelSixSubsection{6}
defl@levelSixSubsection{@dottedtocline{6}{2em}{9em}}
makeatother
%% Number down to levelSixSubsections
setcounter{secnumdepth}{8}
setcounter{tocdepth}{6}
%% Indentation
cftsetindents{section}{2em}{3em}
cftsetindents{subsection}{2em}{4.0em}
cftsetindents{subsubsection}{2em}{5.0em}
cftsetindents{paragraph}{2em}{6.0em}
cftsetindents{subparagraph}{2em}{7.0em}
begin{document}
tableofcontents
section{Section}
levelOneSubsection{levelOneSubsection}
levelTwoSubsection{levelTwoSubsection}
levelThreeSubsection{levelThreeSubsection}
levelFourSubsection{levelFourSubsection}
levelFiveSubsection{levelFiveSubsection}
levelSixSubsection{levelSixSubsection}
end{document}
The definition of @dottedtocline
is as follows:
ifnum #1>c@tocdepth else
vskip z@ @plus .2p@
{leftskip #2relax rightskip
@tocrmarg parfillskip -rightskip parindent #2relax
@afterindenttrue interlinepenalty @M leavevmode
@tempdima #3relax
advance leftskip @tempdima null nobreak
hskip -leftskip {#4}nobreak
%%% dotfill here
leaders hbox {$m@th mkern @dotsep muhbox {.}mkern @dotsep mu$}
hfill nobreak
hb@xt@ @pnumwidth {hfil normalfont normalcolor #5}par }
fi
The part responsible for the dotted line is mkern @dotsep muhbox {.}mkern @dotsep mu
, so a small horizontal space (mkern
) of width @dotsep mu
, then a dot, then another small horizontal space. @dotsep
is defined as 4.5, a mu is a 'math unit' that is 1/18 em, an em is a unit that has the width of an M (in the active font size), so the distance in the dotfill is 1/4th of an M.
You can change this definition using the etoolbox
package. This package provides the command patchcmd
with five arguments: the command to patch, the code in the command that you want to change, the replacement code, and code to be executed when the patch succeeds or fails, respectively (these last two arguments can be left empty).
Replacing the code above with rule{1pt}{0.4pt}
changes the dots of the custom sections into a line:
documentclass[a4paper,11pt]{report}
usepackage{titlesec}
usepackage[utf8]{inputenc}
usepackage[T1]{fontenc}
usepackage[titles]{tocloft}
usepackage{etoolbox}
%% use solid lines instead of dotted lines for toc
renewcommand{cftdot}{rule{1pt}{0.4pt}}
renewcommand{cftdotsep}{0}
makeatletter
patchcmd{@dottedtocline}{mkern @dotsep muhbox {.}mkern @dotsep mu}%
{rule{1pt}{0.4pt}}{}{}
makeatother
Result:
Edit: to make the patched command a bit cleaner you can also replace the entire box with hrule
, as mentioned in the other answer:
patchcmd{@dottedtocline}{hbox {$m@th mkern @dotsep muhbox {.}mkern @dotsep mu$}}%
{hrule}{}{}
Correct answer by Marijn on February 28, 2021
The tocloft
package isn't redefining @dottedtocline
to get its effects, but rather creates its own parameterized versions of the l@SECTION
commands for easy customization. You can dig into the later sections of the tocloft
documentation to see how this works.
Alternatively, you can use a bespoke macro like the following:
def@ruledtocline#1#2#3#4#5{%
ifnum #1>c@tocdepth else
vskip z@ @plus.2p@
{leftskip #2relax rightskip @tocrmarg parfillskip -rightskip
parindent #2relax@afterindenttrue
interlinepenalty@M
leavevmode
@tempdima #3relax
advanceleftskip @tempdima nullnobreakhskip -leftskip
{#4}nobreak
leadershrulehfill
nobreak
hb@xt@@pnumwidth{hfilnormalfont normalcolor #5%
kern-p@kernp@}%
par}%
fi}
which should be drop-in compatible with @dottedtocline
(note, I've not tested this so it might not work).
Answered by Don Hosek on February 28, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP