2

I'm having trouble with the \label feature in figure environments. Initially, the code I was working with would label Figure 1 in Section 4 as Figure 41. I found this solution: https://stackoverflow.com/questions/3865036/using-a-single-count-for-figures-and-tables-in-latex?rq=1, and I implemented that, but I am now running into an issue where LaTex is counting by twos.

\begin{filecontents*}{dummy.csv}
    Col1,Col2,Col3
    34,54,14
    63,40,93
    39,56,88
    15,42,85
    49,10,16
    89,94,59
    42,79,76
    42,90,35
    13,53,13
    10,60,37
    29,38,34
\end{filecontents*}

\documentclass{article}
\usepackage{csvsimple,longtable,booktabs}

\makeatletter
\renewcommand*{\thetable}{\arabic{table}}
\renewcommand*{\thefigure}{\arabic{figure}}
\let\c@table\c@figure
\makeatother 

\begin{document}
    \section{Section 1}
    \subsection{Sample 1}
    \begin{figure}[ht!]
        \caption{This should be table 1}
        \label{This should be table 1}
        \csvreader[
        longtable=lrrrr,
    table head=
    \toprule\bfseries Col1 &\bfseries Col2 &\bfseries  Col3\\ \midrule\endhead
    \bottomrule\endfoot,
    late after line=\\,
    before reading={\catcode`\#=12},after reading={\catcode`\#=6}
    ]{dummy.csv}{1=\One,2=\Two, 3=\Three}{\One & \Two & \Three}
\end{figure}

\begin{figure}[ht!]
    \caption{This should be table 2}
    \label{This should be table 2}
    \csvreader[
    longtable=lrrrr,
    table head=
    \toprule\bfseries Col4 &\bfseries Col5 &\bfseries  Col6\\ \midrule\endhead
    \bottomrule\endfoot,
    late after line=\\,
    before reading={\catcode`\#=12},after reading={\catcode`\#=6}
    ]{dummy.csv}{1=\One,2=\Two, 3=\Three}{\One & \Two & \Three}
\end{figure}

\section{Section 2}\label{section 2}
\subsection{sample 2}
\begin{figure}[ht!]
    \caption{This should be table 3}
    \label{This should be table 3}
    \csvreader[
    longtable=lrrrr,
    table head=
    \toprule\bfseries Col7 &\bfseries Col8 &\bfseries  Col9\\ \midrule\endhead
    \bottomrule\endfoot,
    late after line=\\,
    before reading={\catcode`\#=12},after reading={\catcode`\#=6}
    ]{dummy.csv}{1=\One,2=\Two, 3=\Three}{\One & \Two & \Three}
\end{figure}
\end{document} 
3

This is because of your use of longtable. A longtable is meant to be used as a non-float, since it's set on one page, and should roll over to subsequent pages. Using them includes the use of a \caption, necessarily. So, even though you don't include a \caption as part of the longtable (stemming from \csvreader[longtable=..., ...]), it increments the figure counter.

Since it's not possible to have a longtable work as expected inside a float, it's better to use the tabular option:

enter image description here

\begin{filecontents*}{dummy.csv}
    Col1,Col2,Col3
    34,54,14
    63,40,93
    39,56,88
    15,42,85
    49,10,16
    89,94,59
    42,79,76
    42,90,35
    13,53,13
    10,60,37
    29,38,34
\end{filecontents*}

\documentclass{article}

\usepackage{csvsimple,booktabs}

\makeatletter
\renewcommand*{\thetable}{\arabic{table}}
\renewcommand*{\thefigure}{\arabic{figure}}
\let\c@table\c@figure
\makeatother 

\begin{document}

\section{Section 1}

\begin{figure}[ht!]
  \centering
  \caption{This should be table 1}
  \csvreader[
    tabular=lrrrr,
    table head=
      \toprule\bfseries Col1 & \bfseries Col2 & \bfseries  Col3 \\ \midrule
      \bottomrule,
    late after line=\\,
    before reading={\catcode`\#=12},
    after reading={\catcode`\#=6}
  ]{dummy.csv}
    {1=\One,2=\Two, 3=\Three}{\One & \Two & \Three}
\end{figure}

\begin{figure}[ht!]
  \centering
  \caption{This should be table 2}
  \csvreader[
    tabular=lrrrr,
    table head=
      \toprule\bfseries Col4 & \bfseries Col5 & \bfseries  Col6 \\ \midrule
      \bottomrule,
    late after line=\\,
    before reading={\catcode`\#=12},
    after reading={\catcode`\#=6}
  ]{dummy.csv}
    {1=\One,2=\Two, 3=\Three}{\One & \Two & \Three}
\end{figure}

\section{Section 2}

\begin{figure}[ht!]
  \centering
  \caption{This should be table 3}
  \csvreader[
    tabular=lrrrr,
    table head=
      \toprule\bfseries Col7 & \bfseries Col8 & \bfseries  Col9 \\ \midrule
      \bottomrule,
    late after line=\\,
    before reading={\catcode`\#=12},
    after reading={\catcode`\#=6}
  ]{dummy.csv}
    {1=\One,2=\Two, 3=\Three}{\One & \Two & \Three}
\end{figure}

\end{document}

I also remove the \endhead and \endfoot definitions inside table head and late after line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.