![他のTexファイルから図、キャプション、グラフィックを挿入することは可能ですか?](https://rvso.com/image/330717/%E4%BB%96%E3%81%AETex%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%8B%E3%82%89%E5%9B%B3%E3%80%81%E3%82%AD%E3%83%A3%E3%83%97%E3%82%B7%E3%83%A7%E3%83%B3%E3%80%81%E3%82%B0%E3%83%A9%E3%83%95%E3%82%A3%E3%83%83%E3%82%AF%E3%82%92%E6%8C%BF%E5%85%A5%E3%81%99%E3%82%8B%E3%81%93%E3%81%A8%E3%81%AF%E5%8F%AF%E8%83%BD%E3%81%A7%E3%81%99%E3%81%8B%EF%BC%9F.png)
ファイルが 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:foo
foo
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