¿Es posible insertar una figura, un título o un gráfico desde otro archivo tex?

¿Es posible insertar una figura, un título o un gráfico desde otro archivo tex?

Tengo dos .texarchivos. El primer .texarchivo incluye dos figuras: Fig.1 y Fig.2. ¿Puedo hacer una referencia cruzada de los detalles completos de cada figura en otro archivo tex, incluido el gráfico, el título, el número de figura...? Gracias a todos

el código defile1.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}

En elfile2.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}

Respuesta1

¡¡¡Actualizar!!!

Advertencia: código masivo jugando al golf por delante

He redefinido figureun poco el entorno, captando el contenido de la figura y el título, así como la etiqueta almacenada. Esto guardará un archivo por entorno de figura (¡no por título!) nombrado \jobname.figurenumber.figy almacenará una etiqueta denominada figlabel:foodonde fooestá la etiqueta asignada al título.

En el segundo archivo, utilice \figextref{foo}luego para volver a cargar el contenido almacenado.

La versión anterior usaba el número de figura incorrecto; esto ya está solucionado. El problema con las etiquetas definidas varias veces se debió a un 'espacio de nombres' incorrecto: el storefigpaquete permite especificar un prefijo de controlador para las referencias. Esto debe manejarse \externaldocumentcomo una opción, el ejemplo reffilepara esto. Si driverprefixse omite esta opción storefig , driverse utiliza el prefijo predeterminado.

Un cuello de botella es la dependencia de hyperrefla versión de \labely \newlabel.

... y por supuesto no funcionará para figurelesscifras, es decir, para \captionof{...}cosas.

conductor.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-- el archivo que utiliza la figura almacenada

\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}

Aquí está el storefigarchivo del paquete modificado: permite el uso correcto de referencias externas y recupera el número de figura del archivo externo:

\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

ingrese la descripción de la imagen aquí

información relacionada