2

I am writing a document where I have different types of figures like tables and listings. To get the captions the way I want I wrote the following in the preamble:

\usepackage[font=small, labelfont=bf, justification=raggedright, format=hang]{caption}

\captionsetup[table]{position=below}
\captionsetup[figure]{position=below}

Now I have seen that the captions on listings and tables look different than in the image

different type of captions

Shouldn't the \usepackage[...]{caption} instruction change the style all of the captions in the document?

Minimal example:

\documentclass[
    12pt, % font size
    ngerman, % german umlaute and german hyphenation
    a4paper, % paper format
    oneside, % one-sided pages
]{article}

% Code
\usepackage{listings} % source code
%\usepackage{bera} % optional: just to have a nice mono-spaced font
\usepackage{xcolor}

% colors
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\definecolor{lightgray}{rgb}{.9,.9,.9}
\definecolor{darkgray}{rgb}{.4,.4,.4}
\definecolor{purple}{rgb}{0.65, 0.12, 0.82}
\definecolor{orangered}{rgb}{255, 69, 0}

\lstset
{
    basicstyle=\footnotesize,
    numbers=left,
    numberstyle=\tiny\color{gray},
    stepnumber=2,
    backgroundcolor=\color{white},
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    frame=single,
    rulecolor=\color{black},
    tabsize=3,
    captionpos=b,
    breaklines=true,
    breakatwhitespace=false,
    title=\lstname,
    keywordstyle=\color{blue},
    commentstyle=\color{dkgreen},
    stringstyle=\color{mauve},
    morekeywords={},
    deletekeywords={}
}

\colorlet{punct}{red!60!black}
\definecolor{background}{HTML}{EEEEEE}
\definecolor{delim}{RGB}{20,105,176}
\colorlet{numb}{magenta!60!black}

\lstdefinelanguage{json}{
    basicstyle=\normalfont\ttfamily,
    numbers=left,
    numberstyle=\scriptsize,
    stepnumber=1,
    numbersep=8pt,
    showstringspaces=false,
    breaklines=true,
    frame=lines,
    backgroundcolor=\color{background},
    literate=
    *{0}{{{\color{numb}0}}}{1}
    {1}{{{\color{numb}1}}}{1}
    {2}{{{\color{numb}2}}}{1}
    {3}{{{\color{numb}3}}}{1}
    {4}{{{\color{numb}4}}}{1}
    {5}{{{\color{numb}5}}}{1}
    {6}{{{\color{numb}6}}}{1}
    {7}{{{\color{numb}7}}}{1}
    {8}{{{\color{numb}8}}}{1}
    {9}{{{\color{numb}9}}}{1}
    {:}{{{\color{punct}{:}}}}{1}
    {,}{{{\color{punct}{,}}}}{1}
    {\{}{{{\color{delim}{\{}}}}{1}
    {\}}{{{\color{delim}{\}}}}}{1}
    {[}{{{\color{delim}{[}}}}{1}
    {]}{{{\color{delim}{]}}}}{1},
}

% Tables
\usepackage{booktabs} % horizontal lines in tables
\usepackage{tabularx} % additional table functions (new environent tabularx)
\usepackage{ltablex}
%\usepackage{ltxtable} % long tabularx tables
\usepackage{multirow} % connect multiple rows in tables
\usepackage{array} % additional table functions
\usepackage{ragged2e} % adds function to center, left, right text

% Caption
\usepackage[font=small, labelfont=bf, justification=raggedright, format=hang]{caption}

\captionsetup[table]{position=below}
\captionsetup[figure]{position=below}

\begin{document}


\begin{lstlisting}[language=json,firstnumber=1,caption={Aufbau des JSON-Strings}]
{
"id": {
"board-id": 1,
"command-group-id": 2,
"command-id": 5 
},
"payload": {
}
}
\end{lstlisting}

\begin{table}[ht!]
    \centering
    \begin{tabular}{@{}lccccl@{}}
        \toprule
        Command Name &
        \multicolumn{4}{c}{ID} &
        Payload \\
        \midrule
        SetSpeed &
        1 &
        1 &
        0 &
        1 &
        speed \\
        \bottomrule
    \end{tabular}
    \caption{Auflistung aller Commands für die Kommunikation zwischen Raspberry Pi 1 und 2}
    \label{tab:Komponentenbeschreibung - Kommunikation - Commands}
\end{table}


\end{document}
  • What is the "normal" (or expected) output? I think a minimal working example would help us help you! – ebosi Apr 19 '16 at 9:42
  • 2
    You can shorten your code by saying \captionsetup{position=below} (this will apply the option for all captions). – Ruben Apr 19 '16 at 9:44
  • The problem is, that below the listing, the caption is centered. But under the table it is on the left side, which is the way I want it. – Pascal Apr 19 '16 at 9:52
  • 3
    Use \captionsetup{position=below,singlelinecheck=false}. The singlelinecheck is true by default, making single lined captions centered. – sodd Apr 19 '16 at 10:09
2

To make sure single line captions are not centered, which is the default with caption, you can set singlelinecheck=false inside the \captionsetup command or the options when loading the caption package:

\captionsetup{singlelinecheck=false}

or

\usepackage[singlelinecheck=false]{caption}

Including the singlelinecheck=false in either of the options mentioned above, the output becomes

Output

| improve this answer | |

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.