Генерация \tableofcontents в классе статьи без использования стандартных \section s

Генерация \tableofcontents в классе статьи без использования стандартных \section s

Есть ли способ сгенерировать \tableofcontentsв классе статьи без использования стандартных s, но при этом использовалась \sectionдругая схема нумерации " "? В частности, см. вторую часть ответа Дэвида Карлайла на\sectionКак лучше всего подойти к созданию документа со множеством подразделов?. Стандарт, \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и сопоставить уровни toc со 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}добавляет строку в оглавление и генерирует 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}

введите описание изображения здесь

Связанный контент