# Cross reference in tex code in LyX

I'm working with LyX. Do you know how to apply cross reference when using "Tex code" option in LyX?

That's the example of what I'm writing in Tex code:

\begin{table}[htbp]
\begin{threeparttable}
\caption{Title}\label{...}
\estwide{myfile.tex}{2}{c}
\end{threeparttable}
\end{table}


Where I should put the label in this case so that cross reference option can work?

I hope the question is clear...thank you for your help!

-
I like Werner's answer better than what I'm about to propose. Nontheless... you could put the label in a LyX note outside of the ERT. Because it's in a LyX note it will not be exported in the LaTeX so it won't override the real label. However, LyX will let you cross-reference that label. –  scottkosty Mar 24 '14 at 5:51
You could consider accepting one of the answers by clicking the checkmark next to it, if you feel any of them solved your problem. You could also upvote all answers you feel are useful. Accepting marks the question as solved. (This applies to all of your questions that have been answered.) –  Torbjørn T. Mar 30 '14 at 7:06

You are not using LyX's interface for managing labels and cross-references. As such, if you're using \label{abc} in an ERT (or "TeX code"), then you need to also use an ERT for the reference \ref{abc} as well.

-
Thank you for your help! –  user46589 Mar 27 '14 at 21:35

Just enter the label manually in the "Cross-reference" dialog. LyX will display it with a BROKEN: prefix (as it cannot find the respective label), but it works nevertheless. You have to be a bit careful if editing it later, though.

Using the LyX dialog has the advantage that you can easily use LyX built-in support for formatted references:

If you don't like the BROKEN: thing or have to insert the cross-reference multiple times and consider it as to tedious to do this manually, there is a trick to teach LyX the existence of a label (also suggested in the comment of scottosky): Just have a LyX note at some place inside your document that contains pseudo-labels for all ERT-inserted labels:

-
This works perfectly well, thank's! –  gaborous Jan 5 at 22:23

This is not really answering your question, but providing a workaround for the specific situation.

If you'd like to, you can use a combination of LyX-stuff and ERTs:

1. Add a table float, with Insert --> Float --> Table, or the corresponding button the toolbar.
2. This will give you a table float with a caption. Write your caption, and add the label inside (or just after) it with Insert --> Label, or via the toolbar.

3. Move the cursor before the caption, add an ERT, and write

\begin{threeparttable}

4. Move the cursor after the caption, add a second ERT, and write

\estwide{myfile.tex}{2}{c}
\end{threeparttable}


This method will allow you to use LyX's cross-referencing system.

-
This is nice. It allows for ERT and using LyX's dialogs, but without the hack I suggested in the comment to the question. –  scottkosty Mar 24 '14 at 11:15