경우에 따라 목차 파일(\jobname.toc) 수동 입력이 비어 있음

경우에 따라 목차 파일(\jobname.toc) 수동 입력이 비어 있음

에 따르면.toc 외부 파일 사용, 다음 파일은 목차를 두 번 표시해야 합니다.

\documentclass{report}
\begin{document}
\tableofcontents
\chapter*{Contents (manual input)}
\makeatletter
\input{\jobname.toc}
\makeatother
\chapter{Foo}
\section{Bar}
\end{document}

그러나 수동 입력은 \jobname.toc비어 있습니다.

존재하는 경우 \jobname.toc(예: 이전 파일의 첫 번째 컴파일 후 .tex) 이 수동 입력은 제거되거나 주석 처리되는 즉시 예상되는(비어 있지 않은) 결과를 제공합니다.

  • 어느 하나 \tableofcontents,
  • 또는 \chapter{Foo}.

무슨 일이 일어나고 있는지 이해합니까?

답변1

연결된 질문은 .toc다음과 관련된 파일을 입력하는 것에 관한 것이었습니다.다른문서.

사용하는 시점에서는 \input{\jobname.toc}확실히 비어 있습니다. 왜냐하면 \tableofcontents입력한 다음 쓰기 위해 파일을 열어서 삭제하기 때문입니다.

그런 다음 파일 .toc은 최종 문서에서 다시 채워집니다.

shorttoc실제 문제를 해결하는 패키지를 살펴보십시오 .

그러나 최종 문서에 쓰기 위해 파일 열기를 지연하면 문제를 쉽게 해결할 수 있습니다.

\documentclass{report}

\usepackage{atveryend}

\makeatletter
\def\@starttoc#1{%
  \begingroup
    \makeatletter
    \@input{\jobname.#1}%
    \if@filesw
      \AfterLastShipout{%
        \@ifundefined{tf@#1}{%
           \expandafter\newwrite\csname tf@#1\endcsname
           \immediate\openout \csname tf@#1\endcsname \jobname.#1\relax
        }{}%
      }%
    \fi
    \@nobreakfalse
  \endgroup}
\makeatother

\begin{document}

\tableofcontents
\chapter*{Contents (manual input)}
\InputIfFileExists{\jobname.toc}{}{Missing toc}

\setcounter{tocdepth}{0}
\renewcommand{\contentsname}{Short contents}
\tableofcontents

\chapter{Foo}
\section{Bar}

\end{document}

원하는 만큼 목차를 입력할 수 있다는 것을 알 수 있습니다. 파일이 아직 누락되었을 수 있으므로 \InputIfFileExists사용해야 합니다 ..toc

답변2

 \tableofcontents

toc를 입력한 다음 엽니다.글쓰기를 위해이렇게 하면 원본이 삭제되므로 파일을 쓰는 동안 파일을 입력하므로 운영 체제 파일 버퍼링이 적용됩니다.

답변3

memoir클래스(확장 book및 클래스) 를 사용하면 report문서가 끝날 때까지 toc 파일이 삭제되지 않고 다시 채워지므로 문서 전체에 원하는 만큼 많은 toc를 넣을 수 있습니다.

관련 정보