5

I am currently creating a figure which is filled by longtable inside, why I do this is because I need the caption to be figure, while on the other hand, I need the table environment.

The thing is, the reason I use longtable is I will create a big table, but putting it inside a figure has made it unseparated. Is there a way to do this actually?

So what I already have are: 1.

\begin{longtable}{ccccc}
% Long content
\end{longtable}

This is ok, but I dont have the Figure caption (if it's possible to put this caption somewhere, maybe this will be an easier solution)

2.

\begin{figure}
  \begin{longtable}{ccccc}
  % Long content
  \end{longtable}
\end{figure}

With this, I will have the caption, but the table will always be in a page (they don't want to be separated).

1

2 Answers 2

8

Do not use a float for longtable, but rather the capt-of or caption package and then \captionof{figure}{...} to create the Figure caption. Because longtable does increment the table counter by one you need to reduce it again to ensure proper numbering for following tables.

\documentclass{article}

\usepackage{longtable}
\usepackage{capt-of}

\begin{document}

% Document
\begin{center}
  \begin{longtable}{ccccc}
      a \\
  % Long content
  \end{longtable}%
  \captionof{figure}{Caption text}%
  \addtocounter{table}{-1}%
\end{center}

\end{document}
5
  • 3
    I think you also have to substract 1 from the table counter. I think longtable advances it automatically but I am not a 100% sure.
    – Martin H
    May 12, 2011 at 22:29
  • @MartinH: Thanks. Yes, this is the case. I added it to my answer. May 12, 2011 at 22:34
  • @MartinH, I think the figure caption is not properly aligned in the center!
    – AXL
    Aug 20, 2022 at 15:07
  • I also tested here, and the "table" is not centered.I also tested here, and the "table" is not centered.
    – AXL
    Aug 20, 2022 at 15:37
  • Just a tip: if you use caption package, you will get a starred version of longtable that does not add 1 to the table counter, so using longtable* there is no need to write \addtocounter{table}{-1}.
    – Leone
    Aug 22, 2022 at 15:37
0

A simple way to do this is by using ltcaption package to redefine the float type of longtable, as suggested by this answer.

\documentclass{article}

\usepackage{longtable}
\usepackage{ltcaption}
\renewcommand\LTcaptype{figure} % redefine the float type used by longtable

\begin{document}

\begin{longtable}{ccccc}
    \caption{My long table as a Figure} \\
    Test & test 2 & test 3 & test 4 & test 5\\
    Test & test 2 & test 3 & test 4 & test 5\\
\end{longtable}

\end{document}

One redefinition

Note: There is no need to update the table counter with \addtocounter{table}{-1}.


Bonus

I am not sure if the following procedure causes the document to be unstable, but I was able to abuse this redefinition:

\documentclass{article}

\usepackage{longtable}
\usepackage{ltcaption}

\begin{document}

\listoffigures
\listoftables

\renewcommand\LTcaptype{figure}
\begin{longtable}{ccccc}
    \caption{My first long table as a Figure} \\
    Test & test 2 & test 3 & test 4 & test 5\\
    Test & test 2 & test 3 & test 4 & test 5\\
\end{longtable}

\renewcommand\LTcaptype{table}
\begin{longtable}{ccccc}
    \caption{My second long table as a Table} \\
    Test & test 2 & test 3 & test 4 & test 5\\
    Test & test 2 & test 3 & test 4 & test 5\\
\end{longtable}

\renewcommand\LTcaptype{figure}
\begin{longtable}{ccccc}
    \caption{My third long table as a Figure} \\
    Test & test 2 & test 3 & test 4 & test 5\\
    Test & test 2 & test 3 & test 4 & test 5\\
\end{longtable}

\end{document}

Many redefinitions

2
  • Is there any way this command \renewcommand\LTcaptype{figure} to be local, i.e., defined inside the figure ambient (so we don't need to repeat it every when changing between table and figure ambients) @Leone?
    – AXL
    Aug 20, 2022 at 14:59
  • @AXL I think that if we use figure environment, the whole problem of splitting the longtable across pages will return. However, you can make \renewcommand local by wrapping {} around a specific table. Try putting a { right before \renewcommand\LTcaptype{figure} and a } right after\end{longtable}. The redefinition won't bleed out from this group restricted by the {}.
    – Leone
    Aug 22, 2022 at 15:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .