4

I am trying to remove the excessive whitespace between the bullet points and the second hi.

\documentclass[10pt]{hitec}
\usepackage{listings}
\lstset{
basicstyle=\small\ttfamily,
columns=flexible,
breaklines=true,
showstringspaces=false,
escapeinside={(*@}{@*)} 
}
\usepackage{multicol}
\title{page}
\begin{document}
hi %normal
\begin{lstlisting}
text
\end{lstlisting}
hi % problem, large white space 
\begin{lstlisting}
(*@\begin{multicols}{2}
\begin{itemize}
    \item 1
    \item 2 
\end{itemize}
\end{multicols} @*)
\end{lstlisting}
\end{document}

enter image description here

Note the exsessive whitespace between the bullet points and hi.

0

1 Answer 1

3

This vertical white space is explained by 3 elements:

  1. Space added above the lstlisting environment. You can remove it by setting the aboveskip option to 0pt.
  2. Apparently, escaped TeX code inside lstlisting starts in horizontal mode, and multicols' behavior when called in horizontal mode is to start on a new line. You can trick it by either adding \vspace*{-\baselineskip} before calling multicols, or putting the whole environment inside a \vbox{}.
  3. multicols also adds vertical space above its content. You can remove it by specifying locally \setlength\multicolsep{0pt}, as answered in Reduce vertical spacing itemize and multicols.

You may remove all of it:

Source and all-removed

but what's really "excessive" depends on the results you're looking for. Whitespace number 2 is probably unwanted. If you want the same vertical space as between regular text and multicols, remove 1 and 2:

Removed 1 and 2

If you want the same vertical space as between regular text and lstlisting's text (like the beginning of your example), remove 2 and 3:

Removed 2 and 3

Here is the full working code for these examples:

\documentclass[10pt]{hitec}
\usepackage{listings}
\lstset{
basicstyle=\small\ttfamily,
columns=flexible,
breaklines=true,
showstringspaces=false,
escapeinside={(*@}{@*)} 
}
\usepackage{multicol}
\title{page}
\begin{document}

\noindent Source
\begin{lstlisting}
(*@\begin{multicols}{2}
\begin{itemize}
    \item 1
    \item 2 
\end{itemize}
\end{multicols}@*)
\end{lstlisting}

\noindent Removing all white space
\begin{lstlisting}[aboveskip=0pt]
(*@\vbox{\setlength\multicolsep{0pt}\begin{multicols}{2}
\begin{itemize}
    \item 1
    \item 2 
\end{itemize}
\end{multicols}}@*)
\end{lstlisting}

\noindent Removing 1 and 2 % as if the text was directly followed by multicols
\begin{lstlisting}[aboveskip=0pt]
(*@\vbox{\begin{multicols}{2}
\begin{itemize}
    \item 1
    \item 2 
\end{itemize}
\end{multicols}}@*)
\end{lstlisting}

\noindent Removing 2 and 3 % same space as if it was only text
\begin{lstlisting}
(*@\vbox{\setlength\multicolsep{0pt}\begin{multicols}{2}
\begin{itemize}
    \item 1
    \item 2 
\end{itemize}
\end{multicols}}@*)
\end{lstlisting}
\end{document}

You must log in to answer this question.

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