동일한 \label을 두 번 정의

동일한 \label을 두 번 정의

다른 문서(기타)의 섹션이 자주 호출되는 문서(기본)를 만들고 있습니다. 문서의 동일한 섹션(기타)이 (기본)의 다른 위치에서 호출되는 경우가 많습니다. 내가 가진 문제는 상호 참조를 \label사용할 때 동일한 명령이 다른(추가) 섹션에서 발생하기 때문에 (기타) 섹션이 (메인)으로 다시 호출될 때마다 다시 작성된다는 것입니다. 이 문제를 해결할 방법이 있나요?\ref\label\label

예를 들어, 아래 코드는 내가 원하지 않는 다음 이미지를 제공합니다.

\documentclass{article}
\usepackage{catchfilebetweentags}

\begin{document}

Hello there.

\ExecuteMetaData[example2]{tag}

\ExecuteMetaData[example2]{tag}

Hello there. 

\end{document}

다른 파일인 example2는 다음과 같습니다.

\documentclass{article}
\usepackage{catchfilebetweentags}
\begin{document}

This is some text to fill space. This is some text to fill space.

%<*tag>
\section{Hello}
\subsection{Hello There}\label{1}
\subsubsection{Hello There, as seen in \ref{1}.}
%</tag>

\end{document}

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

내가 원하는 것은 다음과 같습니다.

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

동일한 문서만 불러오고 싶기 때문에 매번 라벨을 변경할 수 없습니다. 내가 생각한 한 가지 해결책은 가져오는 파일을 각각 다른 레이블로 복제하는 것이지만 그렇게 하지 않는 것이 좋습니다(모두 업데이트하는 것은 지루할 것입니다).

답변1

다음 코드 \newdocumentimport접두사각각 \label\ref(및 \pageref)에 순차적입니다. 그렇게 하면 \newdocumentimport새 문서를 가져오기 직전에 호출 하고 모든 \label항목 \ref에 해당 항목이 추가됩니다.접두사. 를 사용하여 고정 접두사를 강제로 적용할 수도 있습니다 \setfixedprefix{<prefix>}.

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

\documentclass{article}

% Counter to keep track of new document imports
\newcounter{newdocumentimport}

\AtBeginDocument{
  \let\oldlabel\label% Store \label in \oldlabel
  \let\oldref\ref% Store \ref in \oldref
  \let\oldpageref\pageref% Store \pageref in \oldpageref
}

% Establish a new document import
\newcommand{\newdocumentimport}{%
  \stepcounter{newdocumentimport}%
  \renewcommand{\label}[1]{\oldlabel{\thenewdocumentimport-##1}}%
  \renewcommand{\ref}[1]{\oldref{\thenewdocumentimport-##1}}%
  \renewcommand{\pageref}[1]{\oldpageref{\thenewdocumentimport-##1}}%
}
\newcommand{\setfixedprefix}[1]{%
  \renewcommand{\label}[1]{\oldlabel{#1-##1}}%
  \renewcommand{\ref}[1]{\oldref{#1-##1}}%
  \renewcommand{\pageref}[1]{\oldpageref{#1-##1}}%
}

\begin{document}

\section{Hello}
\subsection{Hello There}\label{1}
\subsubsection{Hey There, as seen in \ref{1}.}

%same document imported again
\newdocumentimport

\section{Hello}
\subsection{Hello There}\label{1}
\subsubsection{Hey There, as seen in \ref{1}.}

\end{document}

위 옵션은 다음에서도 작동해야 합니다.hyperref, 의 저장 이후 및 \label는 까지 지연됩니다 .\ref\pageref\AtBeginDocument


자동화된 처리 방식의 경우모두다음을 사용하여 가져온 파일catchfilebetweentags, 프리앰블에서 다음을 사용할 수 있습니다.

\usepackage{catchfilebetweentags}
\let\oldExecuteMetaData\ExecuteMetaData
\renewcommand{\ExecuteMetaData}{\newdocumentimport\oldExecuteMetaData}

이것은 앞에 추가됩니다\newdocumentimport이 항목은 이전에모든 \ExecuteMetaData, 아무것도 변경하지 않고도 코드를 그대로 유지할 수 있습니다.

관련 정보