2

I am using the listings package to insert code into my document, using code from a Stack Overflow answer.

In my document, the caption is shifted right by about one tabstop (relative to page number):

enter image description here

I am using a customized LaTeX document class (MastersDoctoralThesis), which I suspect is the issue.

Here is my MWE:

\documentclass[11pt,oneside,english,singlespacing]{MastersDoctoralThesis}

\usepackage{color}
\usepackage{xcolor}
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{caption}

\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}

\begin{document}

Here is some code.

\begin{lstlisting}[label=some-code,caption={Some Code},language=python]
def prioritize():

    while True:

        if ( SOC <= 20 ):
            toggleRelay( relayHigh, 'off' )

\end{lstlisting}

\end{document}
  • your MWE can use that class (it's generally known (notorious one might say:-) so it should be easy to make a MWE, just start from a copy of the document and delete everything except that float and then delete any package not needed for the example. – David Carlisle May 24 '17 at 6:44
  • @DavidCarlisle done... in writing my thesis I've learned a tremendous amount about LaTeX, and gradually realized I should not have used this class. But this code is going in the final section of the final appendix, so a bit late to change it now. – LShaver May 24 '17 at 14:02
3

Some weird interaction which I could debug (if I really had to:-) , but the log tells you that it is 106pt too wide so this works

\DeclareCaptionFormat{listing}{\hspace*{-53pt}\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}\hspace*{-53pt}}

enter image description here

  • If you are interested why this indentation occurs, see the other answer :-) – Johannes_B May 25 '17 at 7:53
2

The template puts a margin to all captions. What you want is to remove that margin for listings captions. Or all captions if you like. Simply add margin=0pt while setting up the captions of your listings.

lshaverListings

\documentclass[11pt,oneside,english,singlespacing]{MastersDoctoralThesis}

\usepackage{blindtext}
\usepackage{mwepage}
\usepackage{xcolor}
\usepackage{listings}% http://ctan.org/pkg/listings

\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white,margin=0pt}

\begin{document}

\blindtext
\begin{figure}
    \caption{\blindtext}
\end{figure}

\begin{lstlisting}[label=some-code,caption={Some Wombat},language=python]
def prioritize():

while True:
\end{lstlisting}

\end{document}
  • yes this should get the tick really:-) – David Carlisle May 25 '17 at 8:20

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.