0

There's another question that provides the answer I think should work (Automated label tagging in figure? Automated labeling?), but even when I directly copy and paste that code, the \ref function just shows my figures being labelled as fig:#1 rather than image_1.

Here's the code that's not working:

\documentclass{article}
\usepackage{graphicx}
\graphicspath{{/FieldNotes/}}
\usepackage[numbers]{natbib}
\usepackage{placeins}
\usepackage{steinmetz}
%\usepackage[margin=2cm]{geometry}
\usepackage{subcaption}

\newcommand\fig[2]{ \begin{figure} \centering \includegraphics[width=.8\textwidth]{#1} \caption{#2} \label{fig:#1} \end{figure} }

\begin{document}

\fig{image_1}{1} % \fig{image_2}{2} %

\end{document}

I'm using the most recent versions of Texmaker and MiKteX

EDIT: I'm asking why when I use \ref the only labels that it's recognising are fig:#1 and fig:#2 rather than the fig:image_1 and fig:image_2 that I think it should be recognising.

EDIT 2: Got it working now, thanks!

Ellie
  • 1
  • are you asking about the \label{fig:image_1} which you set but do not use, as you have no \ref{fig:image_1} or are you asking about \caption{1} which is legal if weird as it produces Figure 1:1 normally you would have \caption{some text describing the figure} ? – David Carlisle Dec 13 '22 at 16:22
  • @DavidCarlisle the \caption works fine (I just used 1 as a quick example caption), but when I then type \ref in the actual body of the document, it only suggests \ref{fig:#1} or \ref{fig:#2} rather than the \ref{fig:image_1} that I think it should produce? – Ellie Dec 13 '22 at 16:32
  • did you run latex twice? – David Carlisle Dec 13 '22 at 16:35
  • @DavidCarlisle Yeah I've run it a few times (quick build) but even after that it's still not recognising fig:image_1 or fig:image_2 – Ellie Dec 13 '22 at 16:37
  • 1
    Do you mean the auto-completion function of your editor? – Qrrbrbirlbel Dec 13 '22 at 16:38
  • Oh you are asking about the editor not about latex??? The editor can only make guesses based on commands it knows, if you hide the label in a definition it will not see it. It may have an editor specific customisation to say the argument of \fig is a label. – David Carlisle Dec 13 '22 at 16:38
  • Ah I've got it working now. The editor itself wasn't showing anything for the reference, and initially latex wasn't recognising it either but I think I might've just spelled something wrong as it's working now! – Ellie Dec 13 '22 at 16:42
  • https://tex.stackexchange.com/questions/224021/redefine-ref-commands-texmaker-ref-auto-completion – David Carlisle Dec 13 '22 at 16:43

1 Answers1

0

It is not too clear what your question is, as you show no \ref but perhaps this will get you started

enter image description here

\documentclass{article}
\usepackage{graphicx}

\newcommand\fig[2]{% \begin{figure}% \centering \includegraphics[width=.6\textwidth]{#1}% \caption{#2}% \label{fig:#1}% \end{figure}% }

\begin{document}

\fig{example-image-a}{An example}% \fig{example-image-b}{Another example}%

See Figures \ref{fig:example-image-a} and \ref{fig:example-image-b}.

\end{document}

David Carlisle
  • 757,742