0

Introduction. I am cross-referencing tables and figures from a supplementary.tex file to a main.tex file, by using the xr package and the latexmkrc file (I copied and pasted the latexmkrc file from Cross referencing with the xr package in Overleaf). In other words, in the main.tex file, I need to "call" the references of figures and tables, which are defined in the supplementary.tex file. My MWE is the following:

supplementary.tex

\documentclass{article}
\usepackage{hyperref}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{cleveref}

\begin{document}

\section{Tables}
\label{supp_table}
\begin{table}[ht]
\centering
  \begin{tabular}{ll}
    \hline material  & T [K] \\ \hline
    Sn               & 3,7   \\
    Pb               & 7,2   \\
    Al               & 1,2   \\ \hline
  \end{tabular}
\caption{My table.}
\label{my_table}
\end{table}

\section{Figures}
\label{supp_fig}
\begin{figure*}[h]
  \centering
  \begin{subfigure}{0.4\textwidth}
    \includegraphics[width=\linewidth]{figure1/test_figure_latex1}
    \caption{Left}
  \end{subfigure}
  \begin{subfigure}{0.4\textwidth}
    \includegraphics[width=\linewidth]{figure1/test_figure_latex2}
    \caption{Right}
  \end{subfigure}
  \caption{My figure.}
  \label{my_figure}
\end{figure*}

\end{document}

enter image description here

main.tex

\documentclass[12pt]{article}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{xr}

\makeatletter
\newcommand*{\addFileDependency}[1]{
\typeout{(#1)}
\@addtofilelist{#1}
\IfFileExists{#1}{}{\typeout{No file #1.}}
}\makeatother
\newcommand*{\myexternaldocument}[1]{%
\externaldocument{#1}%
\addFileDependency{#1.tex}%
\addFileDependency{#1.aux}%
}
\myexternaldocument{supplementary}

\begin{document}

\noindent
In \Cref{supp_table} there is \Cref{my_table}.\\ \\
In \Cref{supp_fig} there is \Cref{my_figure}.

\end{document}

enter image description here

Question. Once I am in the main.tex file, how can I automatically add the word "Supplementary" to the referenced figures and tables which are defined in the supplementary.tex file? (in order to have the following names: "Supplementary Figure" and "Supplementary Table")

Desired Output. My desired is to get automatically the names "Supplementary Figure" and "Supplementary Table" as outputs of the main file as follows: enter image description here In this moment, instead, I am adding "by hand" the word "Supplementary" to each figure and table which were defined in the supplementary file.

Note after the @gusbrs solution.

To people interested in this topic and in the method here kindly provided by @gusbrs: you might be also interested in How can I pass the name of a section from a tex file to another (with "zref-clever")? and in the @gusbrs great solution!

13
  • This looks somewhat familiar. Maybe search through the questions from the last 1 or 2 days in case the solution is already there. Commented Aug 20, 2024 at 9:41
  • Hi @Sam, if you mean this one "Creating a supplementary document with "Supplementary" prefixes (which are displayed in cross-referenced files)", it was mine, that I deleted and re-wrote in a better and clearer way... I did not find other similar questions that were asked in the last 1 or 2 days... :-(
    – Ommo
    Commented Aug 20, 2024 at 11:06
  • I don't remember the exact title, so could very well have been the deleted one. (In the future, maybe you could edit your question instead of reposting, on main meta, there are rumours that self-deleting questions can count towards a possible question ban) Commented Aug 20, 2024 at 11:11
  • Also, I tried to add the following command \renewcommand{\figurename}{Supplementary Figure} in the supplementary.tex file, hoping that the word "Supplementary" could be then displayed in the output of the main.tex file, but it did not work...
    – Ommo
    Commented Aug 20, 2024 at 11:12
  • Ah, good to know about the consequences of the self-deletion of questions... I did not know..... Btw, it is 2-3 days I am trying to solve this issue, but I am not able to make it work.. :-(
    – Ommo
    Commented Aug 20, 2024 at 11:14

1 Answer 1

2

This is how I'd go about this using zref-clever (you may be able to pull off a similar approach using cleveref, but I'm not sure it would be this straightforward to remap the reference types).

supplementary.tex:

\documentclass{article}

\usepackage{zref-clever}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{hyperref}

\zcsetup{
  countertype={
    section=suppsection,
    table=supptable,
    figure=suppfigure,
    % And others you might need...
  },
}

\zcLanguageSetup{english}{
  type = suppsection ,
    Name-sg = Section ,
    name-sg = section ,
    Name-pl = Sections ,
    name-pl = sections ,
  type = suppfigure ,
    Name-sg = Figure ,
    name-sg = figure ,
    Name-pl = Figures ,
    name-pl = figures ,
  type = supptable ,
    Name-sg = Table ,
    name-sg = table ,
    Name-pl = Tables ,
    name-pl = tables ,
  % And others you might need...
}

\begin{document}

\section{Tables}
\label{supp_table}
\begin{table}[ht]
\centering
  \begin{tabular}{ll}
    \hline material  & T [K] \\ \hline
    Sn               & 3,7   \\
    Pb               & 7,2   \\
    Al               & 1,2   \\ \hline
  \end{tabular}
\caption{My table.}
\label{my_table}
\end{table}

\section{Figures}
\label{supp_fig}
\begin{figure*}[h]
  \centering
  \begin{subfigure}{0.4\textwidth}
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{Left}
  \end{subfigure}
  \begin{subfigure}{0.4\textwidth}
    \includegraphics[width=\linewidth]{example-image-b}
    \caption{Right}
  \end{subfigure}
  \caption{My figure.}
  \label{my_figure}
\end{figure*}

\end{document}

main.tex:

\documentclass[12pt]{article}

\usepackage{zref-clever}
\usepackage{zref-xr}
\usepackage{graphicx}
\usepackage{hyperref}

\zexternaldocument{supplementary}

\zcLanguageSetup{english}{
  type = suppsection ,
    Name-sg = Supplementary section ,
    name-sg = supplementary section ,
    Name-pl = Supplementary sections ,
    name-pl = supplementary sections ,
  type = suppfigure ,
    Name-sg = Supplementary figure ,
    name-sg = supplementary figure ,
    Name-pl = Supplementary figures ,
    name-pl = supplementary figures ,
  type = supptable ,
    Name-sg = Supplementary table ,
    name-sg = supplementary table ,
    Name-pl = Supplementary tables ,
    name-pl = supplementary tables ,
  % And others you might need...
}

\begin{document}

\section{References}

In \zcref{supp_table} there is \zcref{my_table}.

In \zcref{supp_fig} there is \zcref{my_figure}.

\bigskip{}

Now, note why using a different reference type for the supplementary
references, even if it requires some work to setup, pays off.

You can get start of sentence, capitalization and other options to work
correctly, and without fuss.  \zcref[S]{supp_table} is a table in
supplementary.tex, for example.

You can mix references from supplementary and main and get proper results:
\zcref{main_table, my_table_main, main_fig, my_figure_main, supp_table,
  my_table, supp_fig, my_figure}.

Etc.

\section{Tables}
\label{main_table}
\begin{table}[ht]
\centering
  \begin{tabular}{ll}
    \hline material  & T [K] \\ \hline
    Sn               & 3,7   \\
    Pb               & 7,2   \\
    Al               & 1,2   \\ \hline
  \end{tabular}
\caption{My table.}
\label{my_table_main}
\end{table}

\section{Figures}
\label{main_fig}
\begin{figure}
  \includegraphics{example-image-b}
  \caption{My figure.}
  \label{my_figure_main}
\end{figure}

\end{document}

enter image description here

8
  • Chapeau! Oh gosh! I am so impressed!! And you can mix references just with \zcref!! I am really grateful!! :-) :-) I don't understand why none asked this question before, since it is quite common to call - from the main file - some tables and figures which are defined in the supplementary file :-)
    – Ommo
    Commented Aug 20, 2024 at 19:15
  • @Ommo I don't think it is a usual to add such name prefix. I'd say, normally, this kind of thing would belong to the reference itself. E.g. as is done with an appendix, so that you'd have things like "section A", "figure "A.2" etc. such that the numbering makes things unambiguous by itself.
    – gusbrs
    Commented Aug 20, 2024 at 19:26
  • Ah ok, but I have seen the thing to add "supplementary" figure/table quite several times... Maybe it depends on the style of the journal... Maybe, just guessing.. Again, thanks a lot for your solution :-)
    – Ommo
    Commented Aug 20, 2024 at 19:29
  • Just a small doubt (if too demanding I can open a new question)... In the supplementary.tex file I have a section called and labelled as follows \section{Tables} \label{supp_table}. Is there a simple way to "pass" the name of the section, i.e. "Table" in this case, to the main.tex file, by using the command \zcref{supp_table}? In that way, instead of displaying the text "supplementary section 1", I would just have the text "Table", i.e. the proper name of the section in the supplementary file.
    – Ommo
    Commented Aug 22, 2024 at 13:26
  • 1
    @Ommo That is very much a different question. Just ask a new one, linking to relevant other questions, and providing a MWE. ;-)
    – gusbrs
    Commented Aug 22, 2024 at 13:50

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.