2

I am typesetting a document where I want to include some code samples. With each code sample, I want to have some introduction text and in some cases a note after the example.

To accomplish this I am using a \begin{figure} and inside of this I have one or more \begin{lstlisting}.

The issue I am having is that before the lstlisting there is a lot of whitspace. In the full document, this makes it hard to see this as a concise block of the document separate from the content. After the lstlisting the amount of space is okay.

I am hoping there is something I can add to \lstset{} in my preamble to fix this.

Here is a screenshot:

screenshot of my issue

\documentclass[letterpaper,twocolumn,10pt]{article}

\usepackage{fontspec,listings,graphicx,usenix,epsfig,textcomp,xcolor}

\makeatletter

\lstset{
    basicstyle=\footnotesize\ttfamily,
    language=html,     
    frame=single,
    tabsize=2,
    title=\lstname,
    escapechar=�,
    inputencoding=utf8,
    extendedchars=true,
    upquote=true,
    breaklines=true, 
    rulecolor=\color{black!30},
    backgroundcolor=\color{gray!10},
    identifierstyle=\color{magenta!50!black},
    stringstyle=\color{blue},
    breaklines=true,
    breakatwhitespace=true,
    framextopmargin=2pt,
    framexbottommargin=2pt, 
}

\begin{document}


\begin{figure}

\textit{\emph{\footnotesize{Code Example for }}}\texttt{\textit{\emph{\footnotesize{example.com:}}}}

\begin{lstlisting}[basicstyle={\scriptsize\ttfamily},language=HTML]
<div id="div1">foo</div> 
\end{lstlisting}


\textit{\emph{\footnotesize{Another code example}}}{\footnotesize \par}

\begin{lstlisting}[basicstyle={\scriptsize\ttfamily},language=HTML]
<div id="div2">bar</div> 
\end{lstlisting}

\caption{Cation Title Goes Here \label{fig:caption-example} }
\end{figure}

\end{document}

I am using XeLaTeX if that matters. I am also open to other ways of arranging this if its not going to be big hassle to manage in Lyx.

2

\lstname is undefined, and probably listings treats as empty/blank and sets nothing for the listing title. However, this still takes up an entire line at the top of each lstlisting. So, remove title=\lstname in your \lstset:

enter image description here

\documentclass{article}

\usepackage{listings,xcolor,textcomp}

\makeatletter

\lstset{
    basicstyle=\footnotesize\ttfamily,
    language=html,     
    frame=single,
    tabsize=2,
    escapechar=�,
    inputencoding=utf8,
    extendedchars=true,
    upquote=true,
    breaklines=true, 
    rulecolor=\color{black!30},
    backgroundcolor=\color{gray!10},
    identifierstyle=\color{magenta!50!black},
    stringstyle=\color{blue},
    breaklines=true,
    breakatwhitespace=true,
    framextopmargin=2pt,
    framexbottommargin=2pt, 
}

\begin{document}

\begin{figure}

\textit{\emph{\footnotesize{Code Example for }}}\texttt{\textit{\emph{\footnotesize{example.com:}}}}

\begin{lstlisting}[basicstyle={\scriptsize\ttfamily},language=HTML]
<div id="div1">foo</div> 
\end{lstlisting}

\textit{\emph{\footnotesize{Another code example}}}

\begin{lstlisting}[basicstyle={\scriptsize\ttfamily},language=HTML]
<div id="div2">bar</div> 
\end{lstlisting}

\caption{Cation Title Goes Here \label{fig:caption-example} }
\end{figure}

\end{document}

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.