0

I have the following code. However whenever I use \ref{PROGRAM:HELLO-WORLD} in text, I found ?? instead of figure number.

How can I solve the problem???

 \begin{figure} 
        \begin{tikzpicture}
          \caption{My Figure}
        \end{tikzpicture}

        \label{PROGRAM:HELLO-WORLD}
 \end{figure}
  • 4
    For the figure numbering to appear correctly you have to insert a \caption in the figure, before the \label. – Phelype Oleinik Sep 22 '18 at 16:05
  • is the anyway other than using a \caption. it adds a figure number under my figure – Sadegh Eskandari Sep 22 '18 at 16:20
  • 2
    But if you don't have the figure number how are you going to mention the figure number in the text? – Phelype Oleinik Sep 22 '18 at 16:22
  • Yes, there is, see the dicussion here. –  Sep 22 '18 at 16:26
  • 1
    I have a tikz picture inside figure and I added the \caption inside a \node. Therefore I don't need a figure number under my figure – Sadegh Eskandari Sep 22 '18 at 16:58
  • Hi @SadeghEskandari. May be you should add a minimal working example (MWE) starting with \documentclass and ending with \end{document}. – Hafid Boukhoulda Sep 22 '18 at 17:18
  • 1
    The recommended way to put a label on a \caption is to include the label in the mandatory argument: \caption{This is my caption\label{fig:foo}}. Make sure to not accidentally include whitespace before or after the \label though. – Skillmon Sep 22 '18 at 17:44
  • Your code throws some errors when embedded into a minimal document. – Skillmon Sep 22 '18 at 17:47
  • \caption has to be outside of the tikzpicture environment. I don't think tikz can accept a caption outside of a minipage and without caption or subcaption package – koleygr Sep 22 '18 at 19:57
  • 1
    @koleygr \node [text width=5cm] {\caption{foo}}; for example would work fine, because the text width setting of the node makes the node into a minipage like box. – Torbjørn T. Sep 23 '18 at 07:21
  • Thanks for the info @TorbjørnT.... I will check it to see how hyperref behaves with this solution. – koleygr Sep 23 '18 at 09:34

1 Answers1

1

It is possible to place a \label without a \caption. The required code (which is also used by \caption) is \refstepcounter{<counter-type>}. So to set a label to your figure you could use \refstepcounter{figure}\label{PROGRAM:HELLO-WORLD}.

\documentclass[]{article}

\usepackage{tikz}

\begin{document}
\begin{figure} 
  \begin{tikzpicture}
    \node at (0,0) {Foo};
  \end{tikzpicture}%
  \refstepcounter{figure}\label{PROGRAM:HELLO-WORLD}%
\end{figure}

\ref{PROGRAM:HELLO-WORLD}
\end{document}

If you already used a \caption (which I can't inside of tikzpicture) you'd get misnumbered labels this way. To compensate the additional step you could do a \addcounter{figure}{-1}, so the labeling code would become:

\addcounter{figure}{-1}\refstepcounter{figure}\label{PROGRAM:HELLO-WORLD}

As @JohnKormylo mentioned a way to put a caption inside of a tikzpicture (surrounding it in a minipage or like in the following a \parbox) here is the proper way of setting the \label in that case:

\documentclass[]{article}

\usepackage{tikz}

\begin{document}
\begin{figure} 
  \begin{tikzpicture}
    \node at (0,0)
      {\parbox{.8\textwidth}{\caption{Foo\label{PROGRAM:HELLO-WORLD}}}};
  \end{tikzpicture}%
\end{figure}

\ref{PROGRAM:HELLO-WORLD}
\end{document}
Skillmon
  • 60,462
  • You could put a caption inside a minipage inside a node inside a tikzpicture. – John Kormylo Sep 22 '18 at 18:58
  • @JohnKormylo haven't thought of that. – Skillmon Sep 22 '18 at 19:01
  • This solution solved my problem. thank you Skillmon – Sadegh Eskandari Sep 22 '18 at 19:19
  • Possibly less cluttered: \node [text width=0.8\textwidth] {\caption{foo\label{bar}}};. (@JohnKormylo) – Torbjørn T. Sep 23 '18 at 07:24
  • @SadeghEskandari if it solved your problem then maybe mark it as the accepted answer? – Skillmon Sep 23 '18 at 11:41
  • @JohnKormylo No, you don't need to set \textwidth=\hsize anywhere. Not sure where you're going with the second part of the comment, you were the one who mentioned minipages in the first place. (I do see now that setting text width doesn't give the same output as a minipage or \parbox though, so one of the latter might be better: https://v2.overleaf.com/read/bvcbkghfgxvg) – Torbjørn T. Sep 23 '18 at 15:31