GeneralProblem with cross-references

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
carol
Posts: 105
Joined: Wed Dec 24, 2008 7:25 pm

Problem with cross-references

Post by carol »

I don't what could disable the cross-reference. I use the cross-reference correctly to the tables or sections but when I compile, I don't get any error message and the cross-reference doesn't get displayed in the text. Any package loading or definition that might disable the cross-reference?

Thanks

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Problem with cross-references

Post by gmedina »

Hi,

can you please post a minimal working example showing the undesired behaviour?
1,1,2,3,5,8,13,21,34,55,89,144,233,...
carol
Posts: 105
Joined: Wed Dec 24, 2008 7:25 pm

Problem with cross-references

Post by carol »

Yes, here it is:

Code: Select all

\documentclass[10pt]{report}    
% Load packages

\usepackage{tabularx}

\begin{document}

Ref to tab \ref{tab:tab}.
    \par
	\begin{table}
    \mbox{
      \begin{tabular}{|c|c|c|}
        \hline \multicolumn{3}{|c|}{My Table}\\ \hline
        A1 & B2  & C3 \\ \hline
        A2 & ... & .. \\ \hline
        A3 & ..  & .  \\ \hline
      \end{tabular}
      }
	\label{tab:tab}
	\end{table}
\end{document}

User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Problem with cross-references

Post by gmedina »

Hi, inside floating environments (table and figure, and those defined by the user) you need to use the \caption command before the \label command, in order to generate the string that will be picked up by the \ref command:

Code: Select all

\documentclass[10pt]{report}    
\usepackage{tabularx}
\usepackage{hyperref}

\begin{document}

Ref to tab \ref{tab:tab}.
\begin{table}
  \begin{tabular}{|c|c|c|}
   \hline \multicolumn{3}{|c|}{My Table}\\ \hline
  A1 & B2  & C3 \\ \hline
  A2 & ... & .. \\ \hline
  A3 & ..  & .  \\ \hline
  \end{tabular}
  \caption{a test table}
  \label{tab:tab}
\end{table}

\end{document}
Compile the document twice.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
carol
Posts: 105
Joined: Wed Dec 24, 2008 7:25 pm

Re: Problem with cross-references

Post by carol »

As you noticed the sentence "Ref to tab" gets displayed after the table but it comes before the table. THis depends on the position of \begin{table}. On the other hand, I can't put \begin{table} inside of \mbox. What would you suggest?
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Problem with cross-references

Post by gmedina »

You can control the positioning of floats using the placement specifiers; most of the times you want to use [!ht]; your code would look something like:

Code: Select all

\begin{table}[!ht]
  \begin{tabular}{|c|c|c|}
   \hline \multicolumn{3}{|c|}{My Table}\\ \hline
  A1 & B2  & C3 \\ \hline
  A2 & ... & .. \\ \hline
  A3 & ..  & .  \\ \hline
  \end{tabular}
  \caption{a test table}
  \label{tab:tab}
\end{table}
Search the board to see how to proceed if you don't want your tables and/or figures to float at all.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
carol
Posts: 105
Joined: Wed Dec 24, 2008 7:25 pm

Problem with cross-references

Post by carol »

Is it possible to cross-refer to section*? If so, why the name of the section is not displayed?

Code: Select all

\documentclass[10pt]{report}    
\begin{document}
refer to the section \ref{sec:my_section}
\section*{my section} \label{sec:my_section}
\end{document}
kostas
Posts: 7
Joined: Wed Nov 03, 2010 9:02 pm

Problem with cross-references

Post by kostas »

Using \label and \ref are not meant to be used when you want to refer to the "title" of a section/table/figure but when you want to refer to the "number" of a section/table/figure. Because your section uses a * to drop the numbering, you get nothing in your output.

Compare this with the following example where I deleted the *.

Code: Select all

\documentclass[10pt]{report}   
\begin{document}

refer to the section \ref{section:my_section}

\section{my section}\label{section:my_section}

\end{document}
The output is this:
examplep.jpg
examplep.jpg (9.47 KiB) Viewed 13207 times

Edit by localghost: Preferably no external links (see Board Rules). Attachments go onto the forum server where possible.
Post Reply