# Lyx cross refernce to figures not working when there is a new line between caption and label

When I put a new line between the caption and label of a figure float, the cross referencing is shown as "Figure ??" in the resulting PDF file. The hyperlink in the PDF however, works just fine. After removal of the newline between the caption and label, the cross referencing is shown properly, i.e. "Figure 1.1". Does that newline somehow disassociates the label from the caption ? My understanding is that, as long as they are all withing the same float, it should work.

The screen shots are bellow:

before edit on Jan 10:

I'm writing a thesis containing several chapters. The master document's documentclass is book(KOMA-script). The chapters are inserted as branches of the master document. Cross referencing to figures are working fine for all chapters except one. In this faulty chapter, the cross references appear as "Figure ??". However, the hyper link within the resulting PDF document works.

I've already checked this, this is not exactly the problem I'm facing.

• Hmmm... we need to be able to reproduce the problem. How about providing the .log? I doubt that the hyperlink would work but the actual reference be incorrect or ??.
– Werner
Jan 10 '14 at 3:40
• Thanks for reminding me to check the log, @Werner. The references to the images were not found in several occasions. A careful examination revealed that I had put a new line between caption and label of some of the images. The figure numbers came back after I removed those newlines. Jan 10 '14 at 18:18
• Could you include a screen capture of the actual figure/caption/new line configuration? Also extract the actual .tex source and post that for the image that you're saying is causing problems.
– Werner
Jan 10 '14 at 18:31
• I've uploaded the screen shots. Now I notice that the TeX source changes quite a bit with just one new line. Jan 10 '14 at 18:56

When you mention "hyperlink to the figure", I assume you're referring to the hyperlink from the LoF. Yes, that's all based on the \caption, which is set correctly. However, as is clear from your example code, inserting a newline in LyX actually creates a separate group around the caption:

\begin{figure}[h]
\noindent \begin{centering}
\includegraphics[<opts>]{<filename>}\caption{<caption>}

\par\end{centering}

\noindent \centering{}\label{<label>}
\end{figure}

From the above we can see that \caption is placed inside a centering group. Now, according to Understanding how references and labels work you'll see that \label uses \@currentlabel - a macro that is updated when you step a counter. In this case the counter is stepped as part of the call to \caption. However, this update is reverted when the group is closed, because the update is only local to the group that its used in.

So it seems you should NOT put an empty line between the \caption and \label in LyX. It's just the way LyX interprets new lines within structures that seems strange. Of course, as a GUI-like editor for LaTeX, it attempts to remove some of the code-like insertions from the user. However, with this intuitiveness comes some assumptions... something that is clearly incorrectly interpreted in this case.

• +1 for your lucid explanation. I just started to realize how significant (and also unnecessary) is a new line in lyx. Looking at the TeX files now, every time I used a new line, a new paragraph was created. That explains why there is so much blank space in my thesis. User doesn't need to put so many new lines to separate the figure from the text, for example. Jan 10 '14 at 20:06
• @Raiyan: While LyX tries to remove the user from the LaTeX side of things, it's always good to see what's actually going on in the back-end (i.e., the code). Do that often...
– Werner
Jan 10 '14 at 20:10