我有两个.tex
文件。第一个.tex
文件包含两个图:图 1 和图 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
是为标题指定的标签。
在第二个文件中使用\figextref{foo}
然后加载存储的内容。
以前的版本使用了错误的图号——现在已修复。多重定义标签的问题是由于错误的“命名空间”造成的——该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}
引用文件-- 使用存储图形的文件
\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