如何获取目录中灵活的左边距?

如何获取目录中灵活的左边距?

我知道使用包titletoc一可以轻松更改目录的格式。但是,我想知道是否存在一些解决方案可以使目录的左边距灵活。所谓“灵活”,我的意思是某一级别的左边距将根据当前标签号而变化。例如,如果存在 \chapter,但没有 \section、\subsection 和 \subsubsection,则直接的 \paragraph 在 TC 中的边距将与普通的 \section 相同。我知道章节标题的边距基本上由包<left>中的选项控制titletoc。所以我的天真想法是将它与一些结合起来\ifthenelse

\documentclass{book}
\usepackage{xifthen}
\usepackage{titletoc}

\setcounter{tocdepth}{4}
\setcounter{secnumdepth}{4}

\newlength{\TOCparagraphskip}
\setlength{\TOCparagraphskip}{9em}

\titlecontents*
    {paragraph}
    [\ifnum\pdfmatch{.[.].[.].[.]0[.].}{\thecontentslabel}=1
        \setlength{\TOCparagraphskip}{7em}%
        \ifnum\pdfmatch{.[.].[.]0[.]0[.].}{\thecontentslabel}=1
            \setlength{\TOCparagraphskip}{3.8em}%
            \ifnum\pdfmatch{.[.]0[.]0[.]0[.].}{\thecontentslabel}=1
                \setlength{\TOCparagraphskip}{1.5em}%
                \ifnum\pdfmatch{0[.]0[.]0[.]0[.].}{\thecontentslabel}=1
                    \setlength{\TOCparagraphskip}{0em}%
                \fi
            \fi
        \fi
    \else
        \setlength{\TOCparagraphskip}{9em}%
    \fi    
    \TOCparagraphskip]{}{\textbullet{}\;}{}{}[\quad]



\begin{document}
    \tableofcontents
    \paragraph{paragraph}


    \chapter{chapter one}

   
\end{document}

但不幸的是,我天真的尝试给出了错误信息。

 (./test.toc
./test.toc:1: Missing number, treated as zero.
<to be read again> 
                   \protect 
l.1 ...aph}{\numberline {0.0.0.0.1}paragraph}{1}{}
                                                  %
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)

./test.toc:1: Illegal unit of measure (pt inserted).
<to be read again> 
                   \protect 
l.1 ...aph}{\numberline {0.0.0.0.1}paragraph}{1}{}
                                                  %
Dimensions can be in units of em, ex, in, pt, pc,
cm, mm, dd, cc, nd, nc, bp, or sp; but yours is a new one!
I'll assume that you meant to say pt, for printer's points.
To recover gracefully from this error, it's best to
delete the erroneous units; e.g., type `2' to delete
two letters. (See Chapter 27 of The TeXbook.)


./test.toc:1: Package calc Error: `\let ' invalid at this point.

See the calc package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.1 ...aph}{\numberline {0.0.0.0.1}paragraph}{1}{}
                                                  %
I expected to see one of: + - * / )

但我已经检查过,无论是否经过我的个人破解,文件.toc实际上都是相同的。

\contentsline {paragraph}{\numberline {0.0.0.0.1}paragraph}{1}{}%
\contentsline {chapter}{\numberline {1}chapter one}{3}{}%
\contentsfinish 

所以,我不知道问题出在哪里。或者,也许其他人有更好的想法来实现此功能?

答案1

以下是我实现您想要的方法。

基本示例:

\documentclass{book}
\usepackage{titletoc}

\setcounter{tocdepth}{4}
\setcounter{secnumdepth}{4}

\newlength{\TOCparagraphskip}
\setlength{\TOCparagraphskip}{9em}

\titlecontents*
    {paragraph}
    [\TOCparagraphskip]{}{\textbullet{}\;}{}{}[\quad]



\begin{document}
    \tableofcontents
    \paragraph{paragraph}

    \chapter{chapter one}
    \addtocontents{toc}{\setlength{\TOCparagraphskip}{1em}}

    \paragraph{paragraph 2}
   
\end{document}

您会看到,但是通过\setlength在 toc 文件中插入各种内容,您可以动态更改左缩进。

现在让我们尝试自动化它。不幸的是,<after code>intitlesec不起作用,因为它导致内容行打印得太早。但是,尝试使用etoolbox附加代码来运行切片命令容易出现意外错误

在下面的解决方案中,我们所做的如下:

  • 我们修补该\paragraph命令以便调用normal\paragraph时,我们检查缩进是否需要更改。如果需要,则发出一个命令\addtocontents来执行此操作。
  • 检查是通过我们定义的布尔切换器完成的。发出 后\addtocontents,我们将重置切换器。
  • 修补该命令的问题较少,因为您不必担心*表格或其他恶作剧。我在下面手动完成了,但我认为使用宏\pretocmdetoolbox可以。
  • 我们修补每个分段命令(\chapter,,,等等;我厌倦了打字,所以你必须自己添加额外的级别)以指示缩进级别已经改变,并存储适当的长度\section\subsection
  • 分段命令现在将布尔切换设置为 true,然后存储适当的长度,以便在\paragraph调用时将正确的长度放置在文件中.toc
  • 布尔切换并非绝对必要:它有助于减小文件大小.toc(否则您\setlength将为每个命令打印一个命令\paragraph)。在下面的示例中,不使用切换时打印 7 次,使用切换时打印 5 次。
\documentclass{book}
\usepackage{titletoc}

\setcounter{tocdepth}{4}
\setcounter{secnumdepth}{4}

\newlength{\TOCparagraphskip}
\setlength{\TOCparagraphskip}{9em}


\titlecontents*
    {paragraph}
    [\TOCparagraphskip]{}{\textbullet{}\;}{}{}[\quad]

\makeatletter
\newif\ifqt@tocskipchanged
\qt@tocskipchangedfalse

% Some helper macros to simplify typing
\newcommand\qt@tocchangepskip[1]{\addtocontents{toc}{\setlength{\TOCparagraphskip}{#1}}}

% Patch the sectioning commands
\let\qt@chap\chapter
\def\chapter{\qt@tocskipchangedtrue\gdef\qt@prepare{\qt@tocchangepskip{1.5em}}\qt@chap}
\let\qt@sec\section
\def\section{\qt@tocskipchangedtrue\gdef\qt@prepare{\qt@tocchangepskip{4em}}\qt@sec}
\let\qt@ssec\subsection
\def\subsection{\qt@tocskipchangedtrue\gdef\qt@prepare{\qt@tocchangepskip{7em}}\qt@ssec}
%% insert additional definitions here for subsubsections etc.
% Patch the paragraph command
\let\qt@para\paragraph
\def\paragraph{\ifqt@tocskipchanged\qt@prepare\fi\qt@tocskipchangedfalse\qt@para}
% In case there are paragraphs outside of traditional chapters. Call this after \tableofcontents
\def\setdefaultparaskip{\qt@tocskipchangedtrue\gdef\qt@prepare{\qt@tocchangepskip{0em}}}
\makeatother

\begin{document}
    \tableofcontents

    \setdefaultparaskip % Only necessary if there is a paragraph before any chapters
    \paragraph{paragraph}

    \chapter{chapter one} 
    \paragraph{paragraph 2}

    \section{Section one}

    \paragraph{paragraph 3}
    
    \section{Section two}

    \paragraph{paragraph 4}

    \paragraph{paragraph 4.5}

    \paragraph{paragraph 4.6}

    \chapter{chapter two}
    \paragraph{paragraph 5}
  
\end{document}

上述代码的输出目录:

在此处输入图片描述

相关内容