2

I have a listing environment, but the space between caption and listing is too large. I could use \vspace to control it, but in this case, I should add the command to all of my listings. Is there a way to change them?

enter image description here]1

\documentclass[12pt]{article}

\usepackage{caption}
\usepackage{tikz}
\usepackage{listings}

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

\DeclareCaptionType{codetype}[Listing][List of mytype]
\newenvironment{code}{}{}

\lstset{ %
    basicstyle=\scriptsize\ttfamily,
    frame=L,
    xleftmargin=\parindent,
}

\lstdefinelanguage{hello}{
        emph={grammar, filter, def, class, end, typedef, type, constraint, sentence},
        emphstyle=\textbf,
        escapechar=\,
        basicstyle=\ttfamily\scriptsize,
        numbers=left, % <---- 
        numberstyle=\ttfamily\scriptsize,
        stepnumber=1,
        numbersep=3pt,
        showstringspaces=false,
        breaklines=true,
        frame=lines,
        framerule=1pt,
        rulesep=1pt,
        framexleftmargin=2em,
        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},
}

\begin{document}
    \begin{figure}[htpb]
    \begin{lstlisting}[language=hello,firstnumber=1]
        function bool priceMatch(produceName, price) = {
             return 
             ( produceName == "apple" &&  0 <= price <= 1000 )
        }
    \end{lstlisting}
    \captionof{codetype}{Type definitions}
    \label{code:function}
    \end{figure}

    \begin{figure}[htpb]
    \begin{lstlisting}[language=hello,firstnumber=1]
        function bool priceMatch(produceName, price) = {
             return 
             ( produceName == "apple" &&  0 <= price <= 1000 )
        }
    \end{lstlisting}
    \vspace{-0.5cm}
    \captionof{codetype}{Type definitions}

    \label{code:function}
    \end{figure}
\end{document}

ADDED

With this change, the gap issue is solved, but the \ref does not work; it is left as ??.

    \begin{lstlisting}[language=hello,firstnumber=1, 
        abovecaptionskip=0pt, caption={Type definitions}, captionpos=b]

enter image description here

  • if you want to control the space between the listing and the caption, you need to put \vspace between the listing and caption. – David Carlisle Oct 13 '16 at 12:45
  • @David Carlisle: Thanks, I revised question. – prosseek Oct 13 '16 at 13:14
2

I was wondering why you didn't use the caption's key of lstlistings with captionpos and abovecaptionskip or belowcaptionskip?

I suggest the following solution with respect to above keys.

\documentclass[12pt]{article}

\usepackage{caption}
\usepackage{tikz}
\usepackage{listings}

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

\DeclareCaptionType{codetype}[Listing][List of mytype]
\newenvironment{code}{}{}

\lstset{ %
    basicstyle=\scriptsize\ttfamily,
    frame=L,
    xleftmargin=\parindent,
}

\lstdefinelanguage{hello}{
        emph={grammar, filter, def, class, end, typedef, type, constraint, sentence},
        emphstyle=\textbf,
        escapechar=\,
        basicstyle=\ttfamily\scriptsize,
        numbers=left, % <---- 
        numberstyle=\ttfamily\scriptsize,
        stepnumber=1,
        numbersep=3pt,
        showstringspaces=false,
        breaklines=true,
        frame=lines,
        framerule=1pt,
        rulesep=1pt,
        framexleftmargin=2em,
        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},
}

\begin{document}
    \begin{figure}[htpb]
    \begin{lstlisting}[language=hello,firstnumber=1]
        function bool priceMatch(produceName, price) = {
             return 
             ( produceName == "apple" &&  0 <= price <= 1000 )
        }
    \end{lstlisting}
    \captionof{lstlisting}{Type definitions}
    \label{code:function}
    \end{figure}

    \begin{figure}[htpb]
    \begin{lstlisting}[language=hello,firstnumber=1, 
        abovecaptionskip=0pt, caption={Type definitions}, captionpos=b, label={code:test}]
        function bool priceMatch(produceName, price) = {
             return 
             ( produceName == "apple" &&  0 <= price <= 1000 )
        }
    \end{lstlisting}
%    \captionof{codetype}{Type definitions}
%    \vspace{-1cm}
    \label{code:function}
    \end{figure}

This is a reference to a code \ref{code:test}.
\end{document}

enter image description here

  • The ref doesn't work with this setup. I revised the question. – prosseek Oct 13 '16 at 13:22
  • use label key of lstlisting and the ref works with it. – javadr Oct 13 '16 at 13:25
  • @prosseek I revised the solution, see that. – javadr Oct 13 '16 at 13:32

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.