포함된 파일에 대해서만 setcounter를 수행하십시오.

포함된 파일에 대해서만 setcounter를 수행하십시오.

현재 상황은 다음과 같습니다. 기본 파일에 Latex 파일을 포함하고 있습니다. 이 파일에는 이미 여러 \section{}-part가 있습니다. 이제 1내 마스터 파일의 전역 계산을 망치지 않고 계산을 다시 시작하고 싶습니다 . 나는 다음을 시도했다:

\subsection{First}
\setcounter{section}{0}
\input{first_file}
\subsection{Second}
\setcounter{section}{0}
\input{second_file}

이는 카운터를 재설정하는 데 효과적이지만 전역 계산을 망칠 수도 있습니다. 글로벌 문서를 엉망으로 만들지 않고 카운터를 재설정하는 방법이 있습니까? 나는 포함된 파일을 변경하고 싶지 않으며, 가급적이면 이식 가능한 솔루션을 갖고 싶습니다.
편집: 엉망진창에 대한 설명: 내가 얻는 것은 다음과 같습니다.

1
2
3
\begin{input}
1
2
\end{input}
3
4
5

내가 원하는 것:

1
2
3
\begin{input}
1
2
\end{input}
4
5
6

답변1

보조 카운터를 정의하여 섹션 번호의 임시 저장에 사용합니다.

그런 다음 \inputreset{file}방정식 번호를 재설정하려는 파일을 입력하는 데 사용하십시오.

그러나 당신은로딩을 시도하자마자 문제가 발생합니다 hyperref.

\newcounter{storedsection}

\newcommand{\inputreset}[1]{%
  \setcounter{storedsection}{\value{section}}%
  \setcounter{section}{0}%
  \input{#1}%
  \setcounter{section}{\value{storedsection}}%
}

답변2

이는 임시 카운터를 사용하여 첫 번째 카운터 값이 사용되기 전에 섹션 카운터 값을 저장합니다 \input. 또한 명령이 입력 카운터 \input로 변경된 후 차례로 카운터가 재설정됩니다.\refstepcountersection

편집하다첫 번째 섹션 카운터 값을 자동으로 저장합니다 \input.

\input몇 가지 단점: 를 사용할 때마다 섹션 카운터가 재설정됩니다. 이것이 문제인 경우 일부 임계값 이후 재설정을 제거할 수 있습니다.

\documentclass{article}

\usepackage{etoolbox}
\usepackage{xpatch}

\usepackage[hypertexnames=false]{hyperref}
\newcounter{inputfilecounter}

\newcounter{storesection}


\xpretocmd{\input}{%
  \ifnumequal{\value{inputfilecounter}}{0}{%
    \setcounter{storesection}{\value{section}}%
  }{}%
  \refstepcounter{inputfilecounter}
}{}{}

\newcommand{\RestoreSectionCounter}{%
  \setcounter{section}{\value{storesection}}%
}

\makeatletter
\@addtoreset{section}{inputfilecounter}
\makeatother

\begin{document}

\tableofcontents

\section{Regular}
\clearpage

\input{firstfile}
\clearpage

\input{secondfile}

\RestoreSectionCounter
\clearpage
\section{Continued regular sections}
\end{document}

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

관련 정보