Menu

HowTo_FloatLegend

Markus Kohm

How to add a legend to a floating environment in addition to caption

Some users would like to have a command that behaves in principle like \caption but does not generate a number and simply prefixes it with “Legend:” regardless of the floating environment in which it is used.

Using the tocbasic statement \DeclareNewTOC, a corresponding definition is a matter of three lines:

\documentclass[ngerman,captions=tableabove]{scrartcl}
\usepackage{booktabs}
\usepackage{babel}
\usepackage{blindtext}

% Here are the three lines mentioned:
\DeclareNewTOC[name={Legend},type=legend,nonfloat]{lgd}
\renewcommand{\legendformat}{\legendname}
\newcommand*{\legend}{\captionbelowof{legend}}

\begin{document}
\blindtext

\begin{table}
  \centering
  \caption{an example table}
  \begin{tabular}{lllll}
    \toprule
    1 & 2 & 3 & 4 & 5 \\
    \midrule
    a & table & with & 5 & columns\\
    a & table & with & 5 & columns\\
    a & table & with & 5 & columns\\
    a & table & with & 5 & columns\\
    \bottomrule
  \end{tabular}
  \legend{This is the legend to the table, which also can consist of multiple lines and then behaves accordingly.}
  \label{tab:example}
\end{table}
\end{document}

As a modification, one could also, for example, eliminate the prefix “Legend:” and change the font:

\documentclass[ngerman,captions=tableabove]{scrartcl}
\usepackage{booktabs}
\usepackage{babel}
\usepackage{blindtext}

\DeclareNewTOC[type=legend,nonfloat]{lgd}
\renewcommand{\legendformat}{}% No number or similar.
\newcommand*{\legend}[1]{%
  \begingroup
    \setkomafont{caption}{\footnotesize}% Change font
    \let\captionformat\relax% no ": "
    \captionbelowof{legend}{#1}% set text
  \endgroup
}

\begin{document}
\blindtext

\begin{table}
  \centering
  \caption{an example table}
  \begin{tabular}{lllll}
    \toprule
    1 & 2 & 3 & 4 & 5 \\
    \midrule
    a & table & with & 5 & columns\\
    a & table & with & 5 & columns\\
    a & table & with & 5 & columns\\
    a & table & with & 5 & columns\\
    \bottomrule
  \end{tabular}
  \legend{This is the legend to the table, which also can consist of multiple lines and then behaves accordingly.}
  \label{tab:example}
\end{table}
\end{document}

Again, the advantage remains that single-line legends are automatically centred if necessary:

\documentclass[ngerman,captions=tableabove]{scrartcl}
\usepackage{booktabs}
\usepackage{babel}
\usepackage{blindtext}

\DeclareNewTOC[type=legend,nonfloat]{lgd}
\renewcommand{\legendformat}{}% No number or similar.
\newcommand*{\legend}[1]{%
  \begingroup
    \setkomafont{caption}{\footnotesize}% change font
    \let\captionformat\relax% no ": "
    \captionbelowof{legend}{#1}% print text
  \endgroup
}

\begin{document}
\blindtext

\begin{table}
  \centering
  \caption{an example table}
  \begin{tabular}{lllll}
    \toprule
    1 & 2 & 3 & 4 & 5 \\
    \midrule
    a & table & with & 5 & columns\\
    a & table & with & 5 & columns\\
    a & table & with & 5 & columns\\
    a & table & with & 5 & columns\\
    \bottomrule
  \end{tabular}
  \legend{This is the one-line legend to the table}
  \label{tab:example}
\end{table}
\end{document}

By the way, all these solutions also create a file with the legends, which can also be used as a directory if necessary. For more details, see the chapter on tocbasic in the KOMA-Script manual.

If, on the other hand, you simply want to put left-justified text under the table, you can save yourself the effort:

\documentclass[ngerman,captions=tableabove]{scrartcl}
\usepackage{booktabs}
\usepackage{babel}
\usepackage{blindtext}

\makeatletter% We use a statement with @ in the name ...
\newcommand*{\legend}[1]{%
  \par\medskip
  \noindent\parbox{\linewidth}{%
    \footnotesize
    \@hangfrom{\textbf{Legend: }}#1
  }\par
}
\makeatother% Because of \makeatletter above.

\begin{document}
\blindtext

\begin{table}
  \centering
  \caption{An example table}
  \begin{tabular}{lllll}
    \toprule
    % And because this is often requested, we centre the column headings here
    \multicolumn1c1 & 
    \multicolumn1c2 & 
    \multicolumn1c3 & 
    \multicolumn1c4 & 
    \multicolumn1c{Column 5} \\
    \midrule
    a & table & with & 5 & columns\\
    a & table & with & 5 & columns\\
    a & table & with & 5 & columns\\
    a & table & with & 5 & columns\\
    \bottomrule
  \end{tabular}
  \legend{This is the multi-line legend to the table, which can be written with the help of
    \texttt{\string\@hangfrom} in a similar hanging manner as
    it is the default for the table headings}.
  \label{tab:example}
\end{table}
\end{document}

The additional \parbox is used in this example, by the way, so that \centering within the floating environment does not affect the legend. Instead of \medskip you can of course also use another vertical distance, for example \smallskip or \bigskip or an explicit specification with \vspace{...}.


Related

Wiki (English): HowTo_Floats

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.