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




    alsodigit = {:},
    keywords = {if, and, or, do, end, receive, def, match, defmodule, use, fn, when},
  rulecolor = \color{black},
  backgroundcolor = \color{white},   
  showtabs = false,
  numbers = left,
  numberstyle = \scriptsize,
  stepnumber = 1,
  showstringspaces = false,


\captionof{lstlisting}{My caption}
\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
  • Welcome to TeX.SE. May 12, 2020 at 15:47

1 Answer 1



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

\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:


\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:


enter image description here

  • 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 .