1

I am running a main.tex file in which I use the \input{} command for my subfiles: subfile1.tex, ... Now, I have some figures in my subfiles, say subfile1.tex, and I want to insert a reference to that figure (both in the subfile1.tex itself and in the main.tex) using a \ref{fig:my_figure} command. Unfortunately this does not work, any clue?

For completeness I drop my code here, this is main.tex:

\documentclass[12pt]{article}
\usepackage{graphicx}

\begin{document}

\input{subfile1.tex}

Look at figure \ref{fig:my_figure}

\end{document}

while this is subfile.tex:

\begin{figure}
\includegraphics[width=4cm]{logo.png}
\label{fig:my_figure}
\end{figure}

Look at figure \ref{fig:my_figure}

Put any image in place of my logo.png and try yourself. In my case, when I run main.tex (in TeXmaker) it shows me neither of the two references.

2
  • Please also note that subfigure and subfile means something completely different in the latex world, so be careful not to mixed them up
    – daleif
    Nov 17 '20 at 12:09
  • thanks! didn't know that
    – guest
    Nov 17 '20 at 12:33
1

The problem her is that the figure env it self does not provide anything the label can refer to. It is the \caption command that provides a figure number that the \label can refer to.

Here is an example:

\begin{filecontents}[overwrite]{sub-\jobname.tex}
\begin{figure}
  \rule{4cm}{4cm}
  \caption{test}
\label{fig:my_figure}
\end{figure}

Look at figure \ref{fig:my_figure}

\end{filecontents}

\documentclass[12pt]{article}
\usepackage{graphicx}

\begin{document}

 \input{sub-\jobname.tex}

Look at figure \ref{fig:my_figure}

\end{document}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.