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

6
  • 1
    Why are you squeezing the listing into a minipage at all?
    – user31729
    Apr 18, 2018 at 19:35
  • I was following this: tex.stackexchange.com/a/54449/161008 Apr 18, 2018 at 19:47
  • The minipage seems unnecessary there, however
    – user31729
    Apr 18, 2018 at 19:48
  • New here to latex, my document might be all cases of ungodly mess. Apr 18, 2018 at 19:51
  • While unnecessary, the minipage has the good side effect to suppress the outside box or environment warning Apr 18, 2018 at 19:53

1 Answer 1

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

6
  • I updated my question, removing the listing from minipage causes issues for other minipages. Apr 18, 2018 at 20:21
  • @HighMans As you can see in my answer, I would not encapsulate the listings in anything and use their own caption mechanism. Apr 18, 2018 at 20:24
  • Sorry, I wasn't clear in my edit. How do you go about making the numbering "Figure X" instead of "Listing X"? Apr 18, 2018 at 20:37
  • @HighMans Please see my updated answer Apr 18, 2018 at 20:42
  • I just noticed, yes -- it shows "Figure X", but it doesn't counting with the other figures. I can change the name to something that suits my document, but was hoping it'd keep the global counter. Apr 18, 2018 at 23:45

You must log in to answer this question.

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