他のTexファイルから図、キャプション、グラフィックを挿入することは可能ですか?

他のTexファイルから図、キャプション、グラフィックを挿入することは可能ですか?

ファイルが 2 つあります.tex。最初の.texファイルには、図 1 と図 2 の 2 つの図が含まれています。グラフィック、キャプション、図番号など、各図の詳細全体を別の tex ファイルで相互参照できますか? よろしくお願いします。

のコードfile1.tex

\documentclass[preprint,12pt, sort&compress]{elsarticle}
\usepackage{graphicx}
\usepackage{caption,subfig} 
\begin{document}
 This is figure 1
\begin{figure} 
  \centering \includegraphics[width=0.5\linewidth]{example-image-a}
  \caption{This is caption 1}
  \label{fig:1}
\end{figure}   

\begin{figure} 
  \centering \includegraphics[width=0.5\linewidth]{example-image-b}
  \caption{This is caption 2}
  \label{fig:2}
\end{figure}

\end{document}

の中にfile2.tex

\documentclass[preprint,12pt, sort&compress]{elsarticle}
\usepackage{graphicx}
\usepackage{caption,subfig}
\usepackage{xcite}
\usepackage{xr-hyper}
\externaldocument{file1}
\begin{document}
Figure \ref{fig:2} is figure in the file1 file. And I want to show it in below
% Figure2
Figure \ref{fig:1} is figure in the file1 file. And I want to show it in below
% Figure1
\end{document}

答え1

アップデート!!!

警告 - 大規模なコードゴルフが控えています

figure環境を少し再定義し、図とキャプションの内容、および保存されたラベルを取得します。これにより、図の環境ごとに (キャプションごとではありません!) という名前のファイルが保存され、キャプションに付けられたラベルである という名前\jobname.figurenumber.figのラベルが保存されます。figlabel:foofoo

2 番目のファイルでは、\figextref{foo}then を使用して保存されたコンテンツを再度読み込みます。

以前のバージョンでは間違った図番号が使用されていましたが、これは修正されました。 複数定義されたラベルの問題は、間違った「名前空間」が原因でした。パッケージでは、storefig参照のドライバー プレフィックスを指定できます。これは\externaldocumentオプションとして処理する必要があります。例を参照してreffileください。この storefig オプションdriverprefixが省略された場合は、デフォルトのプレフィックスdriverが使用されます。

hyperrefボトルネックとなるのは、 の および のバージョン\labelへの依存です\newlabel

...そしてもちろん、figureless数字、\captionof{...}つまり物には機能しません。

ドライバー.tex

\documentclass[preprint,12pt, sort&compress]{elsarticle}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subfig} 
\usepackage[driverprefix=mydriver]{storefig}

\begin{document}
This is figure 1
\begin{figure} 
  \centering \includegraphics[width=0.5\linewidth]{example-image-a}
  \caption{This is caption 1}
  \label{fig:1}
\end{figure}   

\begin{figure} 
  \centering \includegraphics[width=0.5\linewidth]{example-image-b}
  \caption{This is caption 2}
  \label{fig:2}
\end{figure}


\end{document}

reffile.tex-- 保存された図を使用するファイル

\documentclass[preprint,12pt, sort&compress]{elsarticle}
\usepackage{graphicx}
\usepackage{caption,subfig}
\usepackage{xcite}
\usepackage{xr-hyper}
\usepackage[driverprefix=mydriver]{storefig}

\externaldocument[\storefigdriverprefix]{driver}
\begin{document}
Figure \ref{fig:2} is figure in the file1 file. And I what to show it in below

% Now fetch the stuff
\figextref[h]{fig:2}% is figure in the file1 file. And I what to show it in below

\end{document}

変更されたstorefigパッケージ ファイルは次のとおりです。外部参照を正しく使用できるようになり、外部ファイルから図番号が取得されます。

\NeedsTeXFormat{LaTeX2e}

\ProvidesPackage{storefig}
\RequirePackage{xkeyval}
\RequirePackage{xparse}
\RequirePackage{refcount}
\RequirePackage{letltxmacro}
\RequirePackage{tcolorbox}
\RequirePackage{hyperref}

\DeclareOptionX{driverprefix}[driver]{%
  \gdef\storefig@@driverprefix{#1}%
}

\ExecuteOptionsX{driverprefix=driver}
\ProcessOptionsX

\newcommand{\storefigdriverprefix}{%
  \storefig@@driverprefix%
}


\AtBeginDocument{% 
  \typeout{Package storefig info: Prefix is \storefig@@driverprefix}%
  \def\label#1{%
    \@bsphack
    \begingroup
    \def\label@name{#1}%
    \xdef\@lastlabel{#1}% My hack!
    \label@hook
    \protected@write\@auxout{}{%
      \string\newlabel{#1}{%
        {\@currentlabel}%
        {\thepage}%
        {\@currentlabelname}%
        {\@currentHref}{}%
      }%
    }%
    \endgroup
    \@esphack
  }%
}

\LetLtxMacro\latex@@figure\figure
\let\latex@@endfigure\endfigure


\AtBeginDocument{%
  \RenewDocumentEnvironment{figure}{O{ht}}{%
    \latex@@figure[#1]
    \begingroup
    \tcbverbatimwrite{\jobname.\the\numexpr\value{figure}+1.fig}%
  }{\endtcbverbatimwrite\endgroup%
    % Now load it again
    \input{\jobname.\the\numexpr\value{figure}+1.fig}
    \latex@@endfigure%
    % Store a general label to the .aux file
    \immediate\write\@auxout{%
      \string\newlabel{figlabel:\storefig@@driverprefix:\@lastlabel}{{\jobname.\number\value{figure}.fig}{}{}{}{}}%
    }%
  }
}

\NewDocumentCommand{\figextref}{O{tpb}mo}{%
  \begingroup
  \IfValueTF{#3}{%
    \def\local@driverprefix{#3}%
  }{%
    \def\local@driverprefix{\storefig@@driverprefix}%
  }%
  \latex@@figure[#1]
  \renewcommand{\thefigure}{\getrefnumber{\local@driverprefix#2}}% Redefine the figure counter output according to the value delivered by \
  \InputIfFileExists{\getrefnumber{\local@driverprefix figlabel:\storefig@@driverprefix:#2}}{%
    \typeout{Loading file \local@driverprefix figlabel:\storefig@@driverprefix:#2}%
  }{%
    \typeout{Sorry, not found}%
  }
  \latex@@endfigure
  \endgroup
}


\endinput

ここに画像の説明を入力してください

関連情報