在文章类中生成 \tableofcontents 而不使用标准 \section

在文章类中生成 \tableofcontents 而不使用标准 \section

有没有办法\tableofcontents在文章类中生成一个而不使用标准\section,而是使用不同的“\section使用不同的“ ”编号方案?具体来说,请参阅 David Carlisle 对创建包含多个小节的文档的最佳方法是什么?。标准\tableofcontents似乎不起作用。我是 LaTeX 的新手,但我对此感到惊讶,因为\label\ref运行得很好。

编辑:我希望能够创建一个目录,其中包含例如第一级或第一级和第二级标题(就像 \setcounter{tocdepth}{2} 的工作方式一样)。下面是上面引用的代码:

\documentclass{article}

\newcounter{depth}
\setcounter{depth}{1}
\newcounter{pi}
\newcounter{pii} [pi]  \renewcommand\thepii{\thepi.\arabic{pii}}
\newcounter{piii}[pii] \renewcommand\thepiii{\thepii.\arabic{piii}}
\newcounter{piv} [piii]\renewcommand\thepiv{\thepiii.\arabic{piv}}
\newcounter{pv}  [piv] \renewcommand\thepv{\thepiv.\arabic{pv}}


\def\psetdepth#1#2\relax{%
\ifx+#1\relax\addtocounter{depth}{#2}%
\else\ifx-#1\relax\addtocounter{depth}{-#2}%
\else\setcounter{depth}{#1#2}\fi\fi}

\newcommand\sentence[2][+0]{%
  \par
  \psetdepth#1\relax\relax
  \vspace{\csname pspace\roman{depth}\endcsname}%
  \hspace{\csname phspace\roman{depth}\endcsname}%
  \hangindent=\csname pindent\roman{depth}\endcsname
  \hangafter=1
  \indent\refstepcounter{p\roman{depth}}\llap{\csname thep\roman{depth}\endcsname\ }%
  {\csname pstyle\roman{depth}\endcsname#2\par}}

\newcommand\phspacei{20pt}
\newcommand\phspaceii{40pt}
\newcommand\phspaceiii{65pt}
\newcommand\phspaceiv{100pt}
\newcommand\phspacev{140pt}

\newcommand\pindenti{5ex}
\newcommand\pindentii{10ex}
\newcommand\pindentiii{15ex}
\newcommand\pindentiv{23.5ex}
\newcommand\pindentv{32.5ex}

\newcommand\pspacei{15pt}
\newcommand\pspaceii{15pt}
\newcommand\pspaceiii{10pt}
\newcommand\pspaceiv{10pt}
\newcommand\pspacev{10pt}

\newcommand\pstylei{\Large\bfseries}
\newcommand\pstyleii{\bfseries\itshape}
\newcommand\pstyleiii{\normalfont}
\newcommand\pstyleiv{\normalfont}
\newcommand\pstylev{\normalfont}

\begin{document}

\raggedright

\sentence{This is a sentence.}

\sentence{This is another sentence. Just like any other sentence one would think but perhaps not.}


\sentence[+1]{This sentence is similar to the others, but longer. This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.}


\sentence{This is a sentence.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.}


\sentence[+1]{This is another sentence.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.}


\sentence[+1]{This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.}


\sentence{This is a sentence.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.}

\sentence{This is another sentence.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.}

\sentence{This sentence is similar to the others, but longer.}

\sentence[+1]{Hey there how is it going my good friend. Hey there how is it going my good friend.}

\sentence[-2]{This is a sentence hey.}

\sentence{This is another sentence.}

\sentence{This sentence is similar to the others, but longer.}

\sentence[-1]{This is a sentence.}

\end{document}

答案1

可以ToC生成\addcontentsline目录级别到depth计数器值的映射。

代码

\ifcase\c@depth
 \or
  \def\tocstyle{section}
  \or 
  \def\tocstyle{subsection}
  \or
  \def\tocstyle{subsubsection}
  \or
  \def\tocstyle{paragraph}
  \or
  \def\tocstyle{subparagraph}
\fi

\tocstyle根据的值定义宏depth,其中1意味着section等等。

该行\addcontentsline{toc}{\toclevel}{\protect\numberline{}#2}将该行添加到 ToC 并生成hyperlinks(如使用时需要)hyperref

设置tocdepth为的值2将仅允许前两个级别,就像正常的切片级别一样。

\documentclass{article}

\newcounter{depth}
\setcounter{depth}{1}
\newcounter{pi}
\newcounter{pii} [pi]  \renewcommand\thepii{\thepi.\arabic{pii}}
\newcounter{piii}[pii] \renewcommand\thepiii{\thepii.\arabic{piii}}
\newcounter{piv} [piii]\renewcommand\thepiv{\thepiii.\arabic{piv}}
\newcounter{pv}  [piv] \renewcommand\thepv{\thepiv.\arabic{pv}}


\def\psetdepth#1#2\relax{%
\ifx+#1\relax\addtocounter{depth}{#2}%
\else\ifx-#1\relax\addtocounter{depth}{-#2}%
\else\setcounter{depth}{#1#2}\fi\fi}

\setcounter{tocdepth}{2}

\makeatletter
\newcommand\sentence[2][+0]{%
  \par
  \psetdepth#1\relax\relax
  \vspace{\csname pspace\roman{depth}\endcsname}%
  \hspace{\csname phspace\roman{depth}\endcsname}%
  \hangindent=\csname pindent\roman{depth}\endcsname
  \hangafter=1
  \indent\refstepcounter{p\roman{depth}}\llap{\csname thep\roman{depth}\endcsname\ }%
  \ifcase\c@depth
  \or
  \def\tocstyle{section}
  \or 
  \def\tocstyle{subsection}
  \or
  \def\tocstyle{subsubsection}
  \or
  \def\tocstyle{paragraph}
  \or
  \def\tocstyle{subparagraph}
  \fi
  {\csname pstyle\roman{depth}\endcsname#2\addcontentsline{toc}{\tocstyle}{\protect\numberline{\csname thep\roman{depth}\endcsname}#2}\par}} 
\makeatother

\newcommand\phspacei{20pt}
\newcommand\phspaceii{40pt}
\newcommand\phspaceiii{65pt}
\newcommand\phspaceiv{100pt}
\newcommand\phspacev{140pt}

\newcommand\pindenti{5ex}
\newcommand\pindentii{10ex}
\newcommand\pindentiii{15ex}
\newcommand\pindentiv{23.5ex}
\newcommand\pindentv{32.5ex}

\newcommand\pspacei{15pt}
\newcommand\pspaceii{15pt}
\newcommand\pspaceiii{10pt}
\newcommand\pspaceiv{10pt}
\newcommand\pspacev{10pt}

\newcommand\pstylei{\Large\bfseries}
\newcommand\pstyleii{\bfseries\itshape}
\newcommand\pstyleiii{\normalfont}
\newcommand\pstyleiv{\normalfont}
\newcommand\pstylev{\normalfont}

\begin{document}
\tableofcontents
\raggedright

\sentence{This is a sentence.}

\sentence{This is another sentence. Just like any other sentence one would think but perhaps not.}


\sentence[+1]{This sentence is similar to the others, but longer. This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.}


\sentence{This is a sentence.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.}


\sentence[+1]{This is another sentence.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.}


\sentence[+1]{This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.}


\sentence{This is a sentence.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.}

\sentence{This is another sentence.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.This sentence is similar to the others, but longer.}

\sentence{This sentence is similar to the others, but longer.}

\sentence[+1]{Hey there how is it going my good friend. Hey there how is it going my good friend.}

\sentence[-2]{This is a sentence hey.}

\sentence{This is another sentence.}

\sentence{This sentence is similar to the others, but longer.}

\sentence[-1]{This is a sentence.}

\end{document}

在此处输入图片描述

相关内容