0

I want to be able to add the source information below figures and tables ( longtables included). I have used the idea provided in this post, but it gives the following error for longtables:

! Misplaced \noalign.
\caption ->\noalign

Is it possible to fix and have a single command that would for those enviroments?

Here is a minimal code to reproduce it.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{longtable,booktabs}
\usepackage{blindtext}

\newcommand{\source}[1]{ \vspace{1ex}\caption*{\textbf{Source:} {#1}} }

\begin{document}

\begin{table}[h] \caption{Test - Table}\label{t:test} \begin{tabular}{p{0.5\textwidth} p{0.5\textwidth}} \toprule \blindtext & \blindtext \ \bottomrule \end{tabular} \source{Test Test} \end{table}

\newpage

\begin{longtable}{p{0.5\textwidth} p{0.5\textwidth}} \caption{Test - Longtable} \label{lt:test} \ \toprule \blindtext & \blindtext \ \midrule \blindtext & \blindtext \ \midrule \blindtext & \blindtext \ \bottomrule \source{Test longtable 123} \end{longtable}

\newpage

\begin{figure}[h] \centering \includegraphics[width=0.5\textwidth]{example-image-a} \caption{Test123.}\label{fig-img-a} \source{Test image.} \end{figure}

\end{document}

LEo
  • 772
  • 1
    Why not simply use \multicolumn{2}{@{}l}Source …} as the last row of the table? – Bernard Nov 18 '20 at 14:35
  • It compiles fine without the \vspace{1ex} and with \\ after \source{Test longtable 123}. –  Nov 18 '20 at 14:53

1 Answers1

2

The longtable environment does not like the combination of \vspave plus \caption, at least not without an extra \\ between them.

Solution: Do not use \vspave but let \caption do the skip, for example:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{longtable,booktabs}
\usepackage{blindtext}

\newcommand{\source}[1]{ \captionsetup{skip=1ex,position=b}% \caption*{\textbf{Source:} {#1}} }

\begin{document}

\begin{table}[h] \caption{Test - Table}\label{t:test} \begin{tabular}{p{0.5\textwidth} p{0.5\textwidth}} \toprule \blindtext & \blindtext \ \bottomrule \end{tabular} \source{Test Test} \end{table}

\newpage

\begin{longtable}{p{0.5\textwidth} p{0.5\textwidth}} \caption{Test - Longtable} \label{lt:test} \ \toprule \blindtext & \blindtext \ \midrule \blindtext & \blindtext \ \midrule \blindtext & \blindtext \ \bottomrule \source{Test longtable 123} \ \end{longtable}

\newpage

\begin{figure}[h] \centering \includegraphics[width=0.5\textwidth]{example-image-a} \caption{Test123.}\label{fig-img-a} \source{Test image.} \end{figure}

\end{document}