2

MWE:

\documentclass{article}
\usepackage[stable]{footmisc}
\usepackage{footnote}
\usepackage{caption}
\usepackage{hyperref}
\usepackage{listings}\usepackage{genmpage}

\begin{document}
    \begin{minipage}{\linewidth}
        \centering
        \begin{lstlisting}[frame=single]
                mycode;
        \end{lstlisting}
        \captionof{figure}{Short.}
    \end{minipage}

    \medskip

    \begin{minipage}{\linewidth}
        \centering
        \begin{lstlisting}[frame=single]
                mycode;
        \end{lstlisting}
        \captionof{figure}{Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words.}
    \end{minipage}
\end{document}

In the first example, the caption is aligned perfectly in the middle. However, on the second one it isn't aligned in the center at all. It looks like it's left justified despite having a \centering on it.

Edit:

Using figures to encapsulate lstlistings cause minipages to be out of order.

\documentclass{article}
\usepackage[stable]{footmisc}
\usepackage{footnote}
\usepackage[justification=centering]{caption}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{genmpage}
\usepackage{graphicx}


\begin{document}


    \begin{minipage}{\linewidth}
        \centering
        \includegraphics[width=\linewidth]{example-image-a.jpg}
        \captionof{figure}{Figure at \#1.}
    \end{minipage}


     \begin{figure}
        \begin{lstlisting}[frame=single]
            Figure at #2;
        \end{lstlisting}
        \caption{Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words.}
    \end{figure}

    \begin{figure}
        \begin{lstlisting}[frame=single]
            Figure at #3;
        \end{lstlisting}
        \caption{Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words.}
    \end{figure}



\end{document}

The use of minipages for pictures is derived from here: http://tex.stackexchange.com/a/54449/161008

The use of figures for listings is derived from here: https://tex.stackexchange.com/a/19668/161008

1 Answers1

3

This is the default behaviour of the caption package. If the caption is less than one line, it is centred, otherwise it is justified.

If you want all captions to be centred, you can modify the default behaviour with justification=centering.

\documentclass{article}
\usepackage[stable]{footmisc}
\usepackage{footnote}
\usepackage[justification=centering]{caption}
\usepackage{listings}
\usepackage{genmpage}
\usepackage{hyperref}

\renewcommand{\lstlistingname}{Figure}

\begin{document}


    \begin{lstlisting}[frame=single,caption={Short},captionpos=b]
            mycode;
    \end{lstlisting}

  \medskip

    \begin{lstlisting}[frame=single,caption={Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words. Multiple Words.},captionpos=b]
            mycode;
    \end{lstlisting}

\end{document}

enter image description here