마지막에 표현식/매크로를 평가합니다.

마지막에 표현식/매크로를 평가합니다.

처음에 몇 개의 섹션이 있는지 언급하고 싶은 문서가 있다고 가정해 보겠습니다. 예를 들어 다음과 같습니다.

\documentclass{article}
\begin{document}

  Abstract\\There are ... sections in this document.

  \section{Section 1}
  \section{Section 2}
  \section{Section 3}

\end{document}

여기에는 3개의 섹션이 있다고 말하는 매크로(또는 다른 것)가 있습니다.

불행하게도 마지막에 해당 매크로를 평가하는 방법을 찾을 수 없었습니다(3이 보고됨).

이 예에서는 결국 섹션 수를 알고 싶습니다. 어떤 방식으로든 카운터를 사용하는 솔루션이 있을 수 있지만 실제로는 매크로 평가 순서에 영향을 줄 수 있는 솔루션을 찾고 있습니다.

답변1

다음을 사용 \AtEndDocument하고 \AtBeginDocument첫 번째 실행에서 매크로를 설정할 수 있습니다.

\documentclass{article}

\makeatletter
\AtEndDocument{
    \write\@auxout{\string\gdef\string\previousrunsections{\thesection}}%
}
\AtBeginDocument{%
    \ifcsname previousrunsections\endcsname
    \else
        \gdef\previousrunsections{??}%
    \fi
}
\makeatother
\begin{document}

  Abstract
  
  \noindent There are \previousrunsections{} sections in this document.

  \section{Section 1}
  \section{Section 2}
  \section{Section 3}

\end{document}

두 번 이상 실행하면 다음을 얻을 수 있습니다.

여기에 이미지 설명을 입력하세요

더 많은 제어가 필요한 경우 패키지는 etoolbox많은 후크를 제공합니다.

\\PD: 일반 텍스트에서 줄이나 단락을 끝내는 데 사용하지 마세요.!

답변2

를 사용하여 원하는 곳에 총 개수를 입력할 수 있습니다 totcount.

\documentclass{article}
\usepackage{totcount}

\regtotcounter{section}

\begin{document}

\title{Title}
\author{Ömer}

\maketitle

\begin{abstract}
This is the abstract.

There are \total{section} sections in this document.
\end{abstract}

\section{Section 1}

\section{Section 2}

\section{Section 3}

\end{document}

여기에 이미지 설명을 입력하세요

답변3

이는 xyz정보를 저장하기 위해 aux 파일을 사용합니다.

\documentclass{article}
\newcommand\addxyzline[1]{\addtocontents {xyz}{#1}}
\makeatletter
\newcommand\writexyz{\@starttoc{xyz}}
\makeatother
\begin{document}
%\tableofcontents% CAN UNCOMMMENT TO SEE THAT toc WORKS FINE
\noindent Abstract\\There are \writexyz sections in this document.

\section{Introduction}
\section{Next}
\section{Third}
\addxyzline{\thesection}
\end{document}

컴파일 시 .xyz파일에는 숫자(이 경우)가 포함되고 3파일 에는 다음 .aux이 포함됩니다.

\relax 
\@writefile{toc}{\contentsline {section}{\numberline {1}Introduction}{1}\protected@file@percent }
\@writefile{toc}{\contentsline {section}{\numberline {2}Next}{1}\protected@file@percent }
\@writefile{toc}{\contentsline {section}{\numberline {3}Third}{1}\protected@file@percent }
\@writefile{xyz}{3}

따라서 출력은 다음과 같습니다.

여기에 이미지 설명을 입력하세요

참고: 해당 버전은 입력 파일 이름에 관계없이 작동합니다. toc 접근 방식을 사용하지 않으려면 이를 문서 이름에 직접 연결하고 대신 정의할 수 있습니다.

\newcommand\writexyz{\input junk.xyz }

이 경우 문서는junk.tex여야 합니다.

관련 정보