마지막에 표현식 / 매크로 평가
Sep 01 2020
처음에 몇 개의 섹션이 있는지 언급하려는 문서가 있다고 가정합니다. 예를 들면 다음과 같습니다.
\documentclass{article}
\begin{document}
Abstract\\There are ... sections in this document.
\section{Section 1}
\section{Section 2}
\section{Section 3}
\end{document}
여기 ... 3 개의 섹션이 있음을 나타내는 매크로 (또는 다른 것)가 있습니다.
불행히도 나는 그 매크로를 마지막에 평가하는 방법을 찾을 수 없었습니다.
이 예에서는 결국 섹션 수를 알고 싶습니다. 어떤 식 으로든 카운터를 사용하는 솔루션이있을 수 있지만 실제로 매크로 평가 순서에 영향을 줄 수있는 솔루션을 찾고 있습니다.
답변
3 Rmano Sep 01 2020 at 16:12
다음을 사용할 수 있습니다 \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 : \\일반 텍스트에서 줄이나 단락을 끝내는 데 사용하지 마십시오 !
6 egreg Sep 01 2020 at 17:19
을 사용하여 원하는 곳에 총 수를 넣을 수 있습니다 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}
2 StevenB.Segletes Sep 01 2020 at 16:33
이것은 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 여야합니다.