![是否可以從其他 tex 檔案插入圖形、標題、圖形](https://rvso.com/image/330717/%E6%98%AF%E5%90%A6%E5%8F%AF%E4%BB%A5%E5%BE%9E%E5%85%B6%E4%BB%96%20tex%20%E6%AA%94%E6%A1%88%E6%8F%92%E5%85%A5%E5%9C%96%E5%BD%A2%E3%80%81%E6%A8%99%E9%A1%8C%E3%80%81%E5%9C%96%E5%BD%A2.png)
我有兩個.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}
then 載入儲存的內容。
以前的版本使用了錯誤的數字 - 現在已修復。多重定義標籤的問題是由於錯誤的「命名空間」造成的——該storefig
套件允許為引用指定驅動程式前綴。這必須\externaldocument
作為一個選項來處理,而這個例子reffile
就是這樣的。如果driverprefix
省略此 storefig 選項,則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}
參考文件.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