我是 Latex 的新手,我尝试创建一个带有可移动部分编号选项卡的部分,如下所示:
在下面的代码中,我使用了 tikz 和 titlesec 包来格式化所需的部分
\documentclass{report}
\usepackage{totcount}
\usepackage{tikz}
\usepackage{blindtext}
\usetikzlibrary{calc,shapes,arrows,positioning,patterns,fit,decorations.pathmorphing}
\usetikzlibrary{decorations.text}
\usepackage[explicit,calcwidth,compact]{titlesec}
\regtotcounter{section}
\newcommand{\secnum}{\arabic{section}}
\titleformat{\section}{\Large\bfseries}{}{0mm}{\tikz[]{
\node [rectangle,,inner sep=0cm,text width=\textwidth] (A) {#1};
\pgfmathsetmacro\ABC{(\secnum-1)/(\totvalue{section}-1)}
\node [yshift=0.4cm] (B) at ($([xshift=1cm]A.north west)!{\ABC}!([xshift=-1cm]A.north east)$) {\thesection};
\draw [rounded corners] ([yshift=0.2cm]A.north west) -| (B.north west) -- (B.north east) |- ([yshift=0.2cm]A.north east);
\draw ([yshift=-0.2cm]A.south west) -- ([yshift=-0.2cm]A.south east);
}}
\begin{document}
\chapter{First Chapter}
\section{First section}
A short paragraph for first section
\section{Second section}
The second paragraph
\section{Third section}
The third paragraph
\section{Last section}
The last paragraph
\chapter{Second Chapter}
\section{First section}
A short paragraph for first section
\section{Last section}
The Last paragraph
\end{document}
问题是,如果我将 totcount 与节计数器一起使用,那么它只会计算最后一章的节数(我的代码中它计算 2 个)。是否有任何宏可以计算每章的节数?抱歉我的英语比较简单。
答案1
这可能不是最简单的方法,但基本上,您可以使用一个新的辅助文件来存储每个章节。请注意,这需要两次编译运行才能起作用(以及之前的\flushchapter
)\end{document
。
\documentclass{report}
\usepackage{tikz}
\usepackage{blindtext}
\usetikzlibrary{calc,shapes,arrows,positioning,patterns,fit,decorations.pathmorphing}
\usetikzlibrary{decorations.text}
\usepackage[explicit,calcwidth,compact]{titlesec}
\usepackage{etoolbox}
\makeatletter
\IfFileExists{\jobname.chap}{\input{\jobname.chap}}{}
\newwrite\chapfile
\immediate\openout\chapfile=\jobname.chap
\let\origchapter\chapter
\def\flushchapter{\protected@write\chapfile{}{\string\expandafter\string\def\string\csname\space chapter\the\c@chapter\string\endcsname{\the\c@section}}}
\renewcommand\chapter{\flushchapter\origchapter}
\titleformat{\section}{\Large\bfseries}{}{0mm}{\tikz[]{
\node [rectangle,,inner sep=0cm,text width=\textwidth] (A) {#1};
\ifcsdef{chapter\the\c@chapter}{%
\pgfmathsetmacro\ABC{(\the\c@section-1)/(\csname chapter\the\c@chapter\endcsname - 1)}%
}{%
\pgfmathsetmacro\ABC{1}%
}
\node [yshift=0.4cm] (B) at ($([xshift=1cm]A.north west)!{\ABC}!([xshift=-1cm]A.north east)$) {\thesection};
\draw [rounded corners] ([yshift=0.2cm]A.north west) -| (B.north west) -- (B.north east) |- ([yshift=0.2cm]A.north east);
\draw ([yshift=-0.2cm]A.south west) -- ([yshift=-0.2cm]A.south east);
}}
\makeatother
\begin{document}
\chapter{First Chapter}
\section{First section}
A short paragraph for first section
\section{Second section}
The second paragraph
\section{Third section}
The third paragraph
\section{Last section}
The last paragraph
\chapter{Second Chapter}
\section{First section}
A short paragraph for first section
\section{Last section}
The Last paragraph
\flushchapter
\end{document}