
내 LaTeX 문서가 이상하게 작동합니다. 단순화된 버전은 다음과 같습니다.
\documentclass{article}
\begin{document}
\tableofcontents
\newpage
\addcontentsline{toc}{part}{A Part of My Document}
\include{includedfile}
\end{document}
그리고 includedfile.tex
:
\section{My Section Title}
Quack.
분명히 목차에서 해당 부분의 제목이 해당 섹션의 제목보다 앞에 있어야 하지만 그렇지 않습니다! 뭐가 문제 야?
답변1
여러 사람들이 언급한 지연 문제는 TeX가 시간이 \write
될 때까지 모든 명령을 지연시킨다는 것입니다 \shipout
. 어떤 이유로든 즉각적인 가 필요한 경우 를 \write
사용할 수 있습니다 \immediate\write
. 이를 위해 처럼 작동 \addcontentsline
하지만 즉시 aux 파일에 쓰는 간단한 새 매크로가 있습니다.
\documentclass{article}
\newcommand\immediateaddcontentsline[3]{%
\begingroup
\let\origwrite\write
\def\write{\immediate\origwrite}%
\addcontentsline{#1}{#2}{#3}%
\endgroup
}
\begin{document}
\tableofcontents
\newpage
\immediateaddcontentsline{toc}{part}{A Part of My Document}
\include{includedfile}
\end{document}
답변2
이것은 까다로운 작은 문제입니다. 그것은 중요한 면 \include
에서 다르다는 것이 밝혀졌습니다 . \input
단지 몇 개의 s를 추가하는 것이 아닙니다 \clearpage
. 내 생각에 올바른 해결책은 \include
일반적인 명령과 거의 비슷하게 작동하는 사용자 정의 명령을 만드는 것입니다.
\newcommand{\myinclude}[1]{\clearpage\input{#1}\clearpage}
을 사용하면 직접적이든 간접적 이든 파일에 "이것과 저것을 파일에 쓰세요"라는 \addcontentsline
줄을 씁니다 . 그런 다음 파일을 읽고 해당 지침을 따릅니다. 라텍스를 사용하면aux
toc
aux
다시, toc
파일에 올바른 내용이 포함되어 있고 멋진 목차를 얻을 수 있습니다.
그러나 tex \write
명령에는 일종의 지연이 있습니다(이해할 수 없음). \addcontentsline
연속해서 여러 번 사용하면 모두 올바른 순서로 쓰기 스택에 올라가기 때문에 문제가 되지 않습니다. 하지만 여기에 까다로운 부분이 있습니다. \include
를 사용하면분리된 aux
포함할 파일에 대한 파일 및즉시메인 aux
파일에 "다른 aux
파일에서 지침을 찾아보세요"(이상한 지연 없이)라는 명령을 작성합니다. 따라서 \include
바로 뒤에 사용하면 \addcontentsline
"다른 aux
파일을 살펴보세요" 명령이 작성됩니다.~ 전에"파일에 내용 쓰기 toc
" 명령. 따라서 포함된 파일의 모든 내용 항목이 먼저 작성됩니다!
답변3
\include
로 교체하면 저에게 효과적입니다 \input
.
제 생각 \include
에는 챕터용(a \clearpage
등을 강제하는 것)이므로 실제로는 사용하지 않습니다.
답변4
목차 위로 addcontents 줄을 이동해 보세요.
업데이트됨:\addcontentsline
가 와 같은 수준이면 잘못된 순서가 발생합니다 \include
. 해결 방법은 \addcontentsline
포함된 파일에 다음을 포함하는 것입니다 .
\documentclass{article}
\begin{document}
\tableofcontents
\newpage
\include{includedfile}
\include{some-other-file}
\end{document}
내용includedfile.tex
\addcontentsline{toc}{part}{First Part of My Document}
\section{My Section Title}
Quack.