4

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}
4

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

  • 2
    With the lstautogobble package, one could even determine the number of leading spaces automatically: tex.stackexchange.com/questions/19953/… – Daniel Sep 17 '18 at 16:40
  • @Daniel This is a good idea! Would you like to post this as an answer of your own or should I amend my answer? – user36296 Sep 17 '18 at 16:46
  • It is not exactly a different approach so IMHO it would fit better to your answer. – Daniel Sep 17 '18 at 19:18
  • @Daniel I added it to my answer, thanks again for your comment! – user36296 Sep 17 '18 at 19:38

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.