Graphics, Figures & TablesFigure numbering

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Ashiataka
Posts: 12
Joined: Tue Jun 26, 2012 2:38 pm

Figure numbering

Post by Ashiataka »

In order to use figures in the multicols environment, I have defined a new figure environment such that I can use images. Unfortunately when I reference the images i.e. \ref{ALABEL} it incorrectly numbers them based on the section they're in. For example, there are two images which are numbered Figure 6 & Figure 7. However when they're referenced they're both referred to as 4.1 (i.e. the section they're in). Is there something I can do to rectify this?

Thank you.

Code: Select all

\makeatletter
\newenvironment{tablehere}
  {\def\@captype{table}}
  {}

\newenvironment{figurehere}
  {\def\@captype{figure}}
  {}
\makeatother

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

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Figure numbering

Post by cgnieder »

Works for me:

Code: Select all

\documentclass{article}
\usepackage{multicol}
\usepackage{lipsum}

\makeatletter
\newenvironment{tablehere}
  {\def\@captype{table}}
  {}

\newenvironment{figurehere}
  {\def\@captype{figure}}
  {}
\makeatother
\begin{document}

\begin{multicols}{2}
 \section{foobar}
 \lipsum[1]
 \begin{figurehere}
  foo
  \caption{foo}\label{tab:foo}
 \end{figurehere}
 \lipsum[1]
 \begin{figurehere}
  bar
  \caption{bar}
  \label{tab:bar}
 \end{figurehere}
 See figures~\ref{tab:foo} and~\ref{tab:bar}.
 \lipsum[2]
\end{multicols}

\end{document}
Please give a Infominimal working example (similar to mine) that shows the unwanted behaviour.

Regards
site moderator & package author
Ashiataka
Posts: 12
Joined: Tue Jun 26, 2012 2:38 pm

Figure numbering

Post by Ashiataka »

Sorry, does this make it any clearer?

Thank you.

Code: Select all

\documentclass[10pt]{article}
\usepackage[pdftex]{graphicx}
\usepackage{multicol}

\makeatletter
\newenvironment{tablehere}
  {\def\@captype{table}}
  {}

\newenvironment{figurehere}
  {\def\@captype{figure}}
  {}
\makeatother

\begin{document}
\section{A section}

\begin{figurehere}
\begin{center}
\includegraphics[width=0.45\textwidth]{./myfigure} \\ %[1cm]
\end{center}
\label{RL4}
\caption{An incorrectly labeled image, i.e. Figure 6.}
\end{figurehere}
A reference, \ref{RL4}, corresponding to the section number i.e. 4.1.

\begin{figurehere}
\begin{center}
\includegraphics[width=0.45\textwidth]{./my2ndfigure} \\ %[1cm]
\end{center}
\label{RL4}
\caption{Another incorrectly labeled image, i.e. Figure 7.}
\end{figurehere}
A reference, \ref{RL4}, corresponding to the section number i.e. 4.1.

\end{document}
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Figure numbering

Post by cgnieder »

Indeed it does! You need to place \label after \caption. The \caption command is the one that actually steps the underlying counter.

Regards
site moderator & package author
Ashiataka
Posts: 12
Joined: Tue Jun 26, 2012 2:38 pm

Re: Figure numbering

Post by Ashiataka »

Oh excellent, thank you very much.

Very much appreciated :).
Post Reply