2

Any suggestion about how to reduce the vertical space between the bottom line of the caption and the code?

enter image description here

Thanks in advance, Humberto

Here is my current code

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{multicol}
\usepackage{xcolor}
\usepackage{listings}

\usepackage{caption}
\DeclareCaptionFormat{myformat}{#1#2#3\hrulefill}
\captionsetup{format=myformat}
\captionsetup[lstlisting]{position=bottom,format=myformat}

\lstdefinelanguage{Elixir}{
    alsodigit = {:},
    keywords = {if, and, or, do, end, receive, def, match, defmodule, use, fn, when},
  backgroundcolor=\color{white},
  rulecolor = \color{black},
  backgroundcolor = \color{white},   
  basicstyle=\scriptsize\ttfamily,
  showtabs = false,
  numbers = left,
  numberstyle = \scriptsize,
  stepnumber = 1,
  showstringspaces = false,
  breaklines=true,
  comment=[l]{\#},
  numbersep=-8pt, 
  basicstyle=\small\ttfamily,
  columns=fullflexible,
  extendedchars=true,
  frame=none,
}

\begin{document}

\begin{center}
\captionof{lstlisting}{My caption}
\begin{multicols}{2}
\begin{lstlisting}[label=lst:elixir1, language=Elixir]
    def loop({ts_a, ts_b}) do
        state = 
          receive do
            {:msg_a, ts} -> 
                {ts, ts_b}
            {:msg_b, ts} ->  
                {ts_a, ts} 
            {:msg_c, ts} ->
                if ts_b > ts_a do
                    # reaction code
                end
                {0,0}
          end
        loop(state)
    end
\end{lstlisting}
\end{multicols}
\end{center}
\end{document}
1
  • Welcome to TeX.SE. May 12, 2020 at 15:47

1 Answer 1

1

Update:

Firstly, since \captionof is used before lstlisting environment, setting \captionsetup[lstlisting]{position=top, ...} is more appropriate.

This way, the vertical space between \hrulefill and double-columned lstlisting environment comes from:

  • the skip below caption, which is \abovecaptionskip (default value is 10pt), and
  • the skip above multicols environment, which is \multicolsep (default value is 12pt plus 4pt minus 3pt).

By setting

\DeclareCaptionFormat{myformat}{#1#2#3\hrulefill}
\captionsetup[lstlisting]{position=top, skip=-\multicolsep, format=myformat}

you get enter image description here

You can also use pass a glue expression to skip option with the help of calc package, in order to (freely) adjust the skip:

\usepackage{calc}

\DeclareCaptionFormat{myformat}{#1#2#3\hrulefill}
\captionsetup[lstlisting]{position=top, skip=-\multicolsep+5pt, format=myformat}

enter image description here

Original answer:

This might not be an ideal solution, but seems to work:

\DeclareCaptionFormat{myformat}{#1#2#3\hrulefill\vspace{-\medskipamount}}

enter image description here

7
  • Thanks @muzimuzhi Z that solution works for me! πŸ‘πŸ»
    – Humberto
    May 12, 2020 at 16:36
  • @Humberto Hi, some analysis and a more rational solution is added to my answer. May 13, 2020 at 1:34
  • Thanks for the update @muzimuzhi. I really appreciate your help. First I was struggling with the default caption position of multicols listings. Later, using 'caption' I solve the position issue, but I still had the extra space between the caption text and the listing. So you solved my problem πŸ‘πŸ»
    – Humberto
    May 13, 2020 at 3:06
  • @Humberto Perhaps you can upvote my answer? :) May 13, 2020 at 3:37
  • Hi @muzimuzhi Z, I did it, but the system says "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." I'm a new user here πŸ™
    – Humberto
    May 13, 2020 at 4:00

You must log in to answer this question.

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