5

I would like two side-by-side subfigures, both containing an lstlisting environment. The problem is that using it in the manner shown, I get a left margin from the listing that is redundant, and with long lines is quite wasteful. Any suggestions as to how to do this neatly?

MWE:

\documentclass{article}
\usepackage{subcaption}
\usepackage{listings}

\begin{document}

\begin{figure}
    \begin{subfigure}{0.48\textwidth}
    \begin{lstlisting}[language=C,frame=single,breaklines=true]
    printf("hello world !!!") // This is a long line sort of command and it repeats itself
    printf("hello world !!!") // This is a long line sort of command and it repeats itself
    printf("hello world !!!") // This is a long line sort of command and it repeats itself
    \end{lstlisting}
    \caption{Caption a} 
    \end{subfigure}
    \hfill
    \begin{subfigure}{0.48\textwidth}
    \begin{lstlisting}[language=C,frame=single,breaklines=true]
    printf("hello world !!!") // This is a long line sort of command and it repeats itself
    printf("hello world !!!") // This is a long line sort of command and it repeats itself
    \end{lstlisting}
    \caption{Caption b}
    \end{subfigure}
    \caption{Caption}
\end{figure}

\end{document}
Elad Den
  • 3,941

1 Answers1

6

You could use gobble=4 to compensate for the 4 leading spaces.

\documentclass{article}
\usepackage{subcaption}
\usepackage{listings}

\begin{document}

\begin{figure}
    \begin{subfigure}{0.48\textwidth}
    \begin{lstlisting}[language=C,frame=single,breaklines=true,gobble=4]
    printf("hello world !!!") // This is a long line sort of command and it repeats itself
    printf("hello world !!!") // This is a long line sort of command and it repeats itself
    printf("hello world !!!") // This is a long line sort of command and it repeats itself
    \end{lstlisting}
    \caption{Caption a} 
    \end{subfigure}
    \hfill
    \begin{subfigure}{0.48\textwidth}
    \begin{lstlisting}[language=C,frame=single,breaklines=true,gobble=4]
    printf("hello world !!!") // This is a long line sort of command and it repeats itself
    printf("hello world !!!") // This is a long line sort of command and it repeats itself
    \end{lstlisting}
    \caption{Caption b}
    \end{subfigure}
    \caption{Caption}
\end{figure}

\end{document}

This can also be done automatically with the lstautogobble package, as suggest by Daniel in his comment:

\documentclass{article}
\usepackage{subcaption}
\usepackage{listings}

\usepackage{lstautogobble}

\begin{document}

\begin{figure}
    \begin{subfigure}{0.48\textwidth}
    \begin{lstlisting}[language=C,frame=single,breaklines=true,autogobble]
    printf("hello world !!!") // This is a long line sort of command and it repeats itself
    printf("hello world !!!") // This is a long line sort of command and it repeats itself
    printf("hello world !!!") // This is a long line sort of command and it repeats itself
    \end{lstlisting}
    \caption{Caption a} 
    \end{subfigure}
    \hfill
    \begin{subfigure}{0.48\textwidth}
    \begin{lstlisting}[language=C,frame=single,breaklines=true,autogobble]
    printf("hello world !!!") // This is a long line sort of command and it repeats itself
    printf("hello world !!!") // This is a long line sort of command and it repeats itself
    \end{lstlisting}
    \caption{Caption b}
    \end{subfigure}
    \caption{Caption}
\end{figure}

\end{document}

enter image description here