在子图中使用 lstlisting

在子图中使用 lstlisting

我想要两个并排的子图,都包含一个 lstlisting 环境。问题是,按照所示方式使用它,我会从列表中得到一个多余的左边距,而长行则非常浪费。有什么建议可以巧妙地做到这一点吗?

梅威瑟:

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

答案1

您可以用它gobble=4来补偿前导 4 个空格。

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

这也可以通过包自动完成lstautogobble,如建议的那样丹尼尔在他的评论中

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

在此处输入图片描述

相关内容