在目录中添加每章图片和条目组

在目录中添加每章图片和条目组

朋友们,我有一本旧的解析几何书,里面有一个不寻常的目录:对于基于当前章节的每一组条目,都有一张图片。好吧,让我展示一些实际的代码:

\documentclass{book}

\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{lipsum}

\begin{document}

\tableofcontents

\chapter{Coordinates of points}

\section{Rectangular coordinates}

\lipsum[1]

\section{Projections of a segment on the axes}

\lipsum[2]

\section{Distance between two points}

\lipsum[3]

\section{The mid-point of a segment}

\lipsum[4]

\section{Division of a segment in any ratio}

\lipsum[5]

\section{Oblique coordinates}

\lipsum[6]

\chapter{The locus of an equation}

\section{First illustrations}

\lipsum[1]

\section{Curve plotting}

\lipsum[2]

\section{Test that a point lie on a curve}

\lipsum[3]

\section{Intercepts}

\lipsum[4]

\section{Points of intersection of two curves}

\lipsum[5]

\section{Oblique coordinates}

\lipsum[6]

\chapter{The straight line}

\section{Equation in terms of point and slope}

\lipsum[1]

\section{Line through two points}

\lipsum[2]

\section{The general equation of first degree}

\lipsum[3]

\section{Parallel and perpendicular lines}

\lipsum[4]

\section{Angle between two lines}

\lipsum[5]

\section{Distance from a point to a line}

\lipsum[6]

\end{document}

生成的目录符合预期:

目录1

现在,这本书有一个与此类似的目录:

目录

我很好奇是否有一种半自动的方法可以将每章图像包含在目录中。我可以发誓这本几何书最初是用 LaTeX 排版的!对这个结果有什么想法或建议吗?:-)

答案1

这是在目录中制作此布局的首次尝试。

titletoc包裹提供强大的\titlecontents宏,允许在文档中途更改分段标题的 ToC 条目。在下面的 MWE 中,使用以下宏定义两个新宏:xparse界面\NewDocumentCommand。具体来说,它们是\rtocstuff[<gap>]{<content>}[<width>]和 的补充\ltocstuff[<width>]{<content>}[<gap>]。前者排版<content>在章节定义的 ToC 组的右侧,而后者排版<content>在左侧。<width>(默认150pt)指定 的宽度<content>(使用重叠以允许不同的宽度),而指定和 ToC<gap>之间的距离。<content>

为了说明起见,我添加了

\rtocstuff{\includegraphics[valign=T,width=50pt]{tiger}}%

到每个奇数章节,

\ltocstuff{\includegraphics[valign=T,width=50pt]{tiger}}%

至偶数章。

这两个命令背后的主要思想是在排版章节目录时调整目录边距。这是通过使用固定长度的框(以获得准确的间距和水平对齐)来实现的。此外,还可以对其进行<content>编辑\smash,以便它可以向上/向下突出到分段目录标题中。

当前的实现可能被认为有点原始,但它可以通过一些技巧来实现adjustboxvalign=T选项。此外,可以优化重叠框内的对齐方式<content>以及目录布局。它还只修改章节和章节目录样式,而不修改其他内容。因此,如果您需要子部分(或其他部分),则需要将它们包括在内以保持一致性。但这可能完全取决于上下文。下面注释繁多的代码将很好地指示应该/可以修改什么以获得所需的输出。

\documentclass{book}

\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{geometry}% http://ctan.org/pkg/geometry
\usepackage{titletoc}% http://ctan.org/pkg/titletoc
\usepackage[export]{adjustbox}% http://ctan.org/pkg/adjustbox
\usepackage{xparse}% http://ctan.org/pkg/xparse

\NewDocumentCommand{\rtocstuff}{O{20pt} m O{150pt}}{% \rtocstuff[<gap>]{<content>}[<width>]
    \titlecontents{chapter}
      [0pt]% left margin indent
      {\bigskip\bfseries}% chapter ToC formatting
      {\makebox[1.5em][l]{\thecontentslabel}}% chapter label (numbered)
      {\hspace*{1.5em}}% chapter label (unnumbered)
      {\titlerule*[1pc]{.}% dotted contents line
       \thecontentspage% ToC page number
       \hspace*{#1}% gap between page number & <content>
       \smash{% remove vertical height of image
         \raisebox{1.5ex}{% align with top of character
           \hspace*{#3}% space for <content>
           \llap{% left overlap
             #2% actual <content>
      }}}}%
    \titlecontents{section}
      [0pt]% left margin indent
      {\normalfont}% section ToC formatting
      {\hspace*{1.5em}\makebox[2.3em][l]{\thecontentslabel}}% section label (numbered)
      {\hspace*{3.8em}}% section label (unnumbered)
      {\titlerule*[1pc]{.}% dotted contents line
       \thecontentspage% ToC page number
       \hspace*{#1}% gap between page number & <content>
       \hspace*{#3}% gap for <content>
      }
}
\NewDocumentCommand{\ltocstuff}{O{150pt} m O{20pt}}{% \ltocstuff[<width>]{<content>}[<gap>]
    \titlecontents{chapter}
      [0pt]% left margin indent
      {\bigskip\bfseries}% chapter ToC formatting
      {\smash{% remove vertical height of image
         \raisebox{1.5ex}{% align with top of character
           \rlap{% right overlap
             #2% actual content
           }\hspace*{#1}% space for <content>
       }}%
       \hspace*{#3}% gap between <content> and ToC entries
       \makebox[1.5em][l]{\thecontentslabel}}% chapter label (numbered)
      {\smash{% remove vertical height of image
         \raisebox{1.5ex}{% align with top of character
           \rlap{% right overlap
             #2% actual <content>
           }\hspace*{#1}% space for <content>
       }}%
       \hspace*{1.5em}}% chapter label (unnumbered)
      {\titlerule*[1pc]{.}% dotted contents line
       \thecontentspage% ToC page number
      }
    \titlecontents{section}
      [0pt]% left margin indent
      {\normalfont}% section ToC formatting
      {\hspace*{#1}% space for <content>
       \hspace*{#3}% gap between <content> and ToC entries
       \hspace*{1.5em}\makebox[2.3em][l]{\thecontentslabel}}% section label (numbered)
      {\hspace*{3.8em}}% section label (unnumbered)
      {\titlerule*[1pc]{.}% dotted contents line
       \thecontentspage% ToC page number
      }
}
    
\usepackage{lipsum}% http://ctan.org/pkg/lipsum

\begin{document}

\contentsmargin{0pt}% Remove right margin in ToC
\tableofcontents

\rtocstuff{\includegraphics[valign=T,width=75pt]{tiger}}
\chapter{Coordinates of points}

\section{Rectangular coordinates} \lipsum[1]
\section{Projections of a segment on the axes} \lipsum[2]
\section{Distance between two points} \lipsum[3]
\section{The mid-point of a segment} \lipsum[4]
\section{Division of a segment in any ratio} \lipsum[5]
\section{Oblique coordinates} \lipsum[6]

\ltocstuff{\includegraphics[valign=T,width=75pt]{tiger}}
\chapter{The locus of an equation}

\section{First illustrations} \lipsum[1]
\section{Curve plotting} \lipsum[2]
\section{Test that a point lie on a curve} \lipsum[3]
\section{Intercepts} \lipsum[4]
\section{Points of intersection of two curves} \lipsum[5]
\section{Oblique coordinates} \lipsum[6]

\rtocstuff{\includegraphics[valign=T,width=75pt]{tiger}}
\chapter{The straight line}

\section{Equation in terms of point and slope} \lipsum[1]
\section{Line through two points} \lipsum[2]
\section{The general equation of first degree} \lipsum[3]
\section{Parallel and perpendicular lines} \lipsum[4]
\section{Angle between two lines} \lipsum[5]
\section{Distance from a point to a line} \lipsum[6]

\end{document}

在此处输入图片描述

最后,由于此示例有重复老虎在每一章中,你还可以使用etoolbox包裹,通过修补\chapter

\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
...
\preto{\chapter}{%
  \ifodd\thechapter
    \ltocstuff{\includegraphics[valign=T,width=75pt]{tiger}}
  \else
    \rtocstuff{\includegraphics[valign=T,width=75pt]{tiger}}
  \fi
}

上述添加产生的输出与之前相同。但是,您可能对这样的实现不感兴趣,因为理想情况下,图像会随着章节的不同而变化,以展示该章节中传达的一些概念性想法。

您还会注意到,我使用 提供的符号完全重新定义了章节和节的格式titlesec。例如,1pc虚线内容行。与往常一样,可以对其进行修改以匹配常规book文档类接近一点了。

相关内容