同じ\labelを2回定義する

同じ\labelを2回定義する

私は、他のドキュメント (others) のセクションが頻繁に呼び出されるドキュメント (main) を作成しています。ドキュメント (others) の同じセクションが、(main) の別の場所で呼び出されることがよくあります。問題は、相互参照に と を使用する場合、\label同じコマンドが別の (さらに先の) セクションで発生するため、(others) セクションが (main) に再度呼び出されるたびに が書き換えられることです。これを回避する方法はありますか?\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 つは、インポートするファイルの複製を作成し、それぞれに異なるラベルを付けることですが、私はそれを実行したくありません (すべてを更新するのは面倒です)。

答え1

次のコードは\newdocumentimport接頭辞それぞれに、\labelそして\ref(そして)が順番に続きます。こうすることで、新しいドキュメントをインポートする直前に\pageref呼び出すだけで、すべてのsとerencesに\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何も変更せずにコードをそのまま残すことができます。

関連情報