1

I am using caption of with block paragraphs. Once a caption has been displayed there is no gap between paragraphs. Can anyone help?

\documentclass[a4paper,10pt]{article}
\usepackage{parskip}
\usepackage{lipsum}
\usepackage[skip=-5pt, font=scriptsize]{caption}
\usepackage{listings}
\usepackage{color}
\usepackage{float}

\newfloat{Snippet}{th}{snippets}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{frame=tb,
  language=Python,
  basicstyle={\scriptsize\ttfamily},
  numbers=none,
  numberstyle=\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  tabsize=4
}

\begin{document}
    \begin{lstlisting}
        primary_colours = ["red", "blue", "yellow"]
    \end{lstlisting}
\lipsum[2]
\lipsum[2]
    \begin{lstlisting}
        primary_colours = ["red", "blue", "yellow"]
    \end{lstlisting}
    \captionof{Snippet}{\texttt{primary\_colours}}
\lipsum[2]
\lipsum[2]

\end{document}
  • 1
    Hi, you get a message by LaTeX: \captionof outside box or environment. See the caption package documentation for explanation. All changes made by \captionof are not trapped inside an environment (like minipage). – Johannes_B Apr 23 '17 at 14:12
  • It is intentional: \setcaptiontype used by \captionof resets the \parskip, \parindent and even calls \sloppy. So you must use them inside an environment as Johannes wrote. But I don't think that it is a good behaviour to change the typesetting environment in this way. I suggest that you write the author. – Ulrike Fischer Apr 23 '17 at 14:24
  • Thanks. That fixed it. I'll check that the environment gives what I want – Psionman Apr 23 '17 at 15:04
1

Maybe you can use option caption and captionpos for the lstlisting environment:

\documentclass[a4paper,10pt]{article}
\usepackage{parskip}
\usepackage{lipsum}
\usepackage[skip=-5pt, font=scriptsize]{caption}
\usepackage{listings}
\usepackage{color}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{frame=tb,
  language=Python,
  basicstyle={\scriptsize\ttfamily},
  numbers=none,
  numberstyle=\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  tabsize=4
}

\renewcommand\lstlistingname{Snippet}

\begin{document}
    \begin{lstlisting}
        primary_colours = ["red", "blue", "yellow"]
    \end{lstlisting}
\lipsum[2]
\lipsum[2]
    \begin{lstlisting}[caption={\texttt{primary\_colours}},captionpos=b]
        primary_colours = ["red", "blue", "yellow"]
    \end{lstlisting}
\lipsum[2]
\lipsum[2]
\end{document}

enter image description here

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.