是否可以在 \begin{lstlisting} 之前添加 \hspace?

是否可以在 \begin{lstlisting} 之前添加 \hspace?

您好,我想hspace在某个\begin{lstlisting}部分之前添加一些内容,以便我的代码可以列在右边。这不起作用:

\hspace{4mm}\begin{lstlisting}
dateRangeInput("daterange", "Select Date", start = min(OLS.Data$Date), 
end = max(OLS.Data$Date))
\end{lstlisting}

答案1

两种可能性:

无论是键xleftmargin=。您还必须通过\captionsetup{margin=}from更改标题边距caption,或者将\stlistings环境嵌套在adjustwidth环境中(来自changepage)。

\documentclass{article}
\usepackage[utf8]{inputenc}
 \usepackage{listings}
 \usepackage{changepage} 
 \lstset{language=C}
\usepackage{caption}
\captionsetup[lstlisting]{singlelinecheck = off, format = plain}

 \begin{document}

\section{A first section}

Some text. Some more text. Some more text. Some more text. Some more text. Some more text. Some more text. Some more text. Some more text. 

{\captionsetup[lstlisting]{margin = 4.8mm}
\begin{lstlisting}[caption ={My caption}, label = lst1, xleftmargin=4.8mm]
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write('Case insensitive ');
Write('Pascal keywords.');
\end{lstlisting} 
}

\begin{adjustwidth}{4.8mm}{}
\begin{lstlisting}[caption ={My caption}, label = lst1]
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write('Case insensitive ');
Write('Pascal keywords.');
\end{lstlisting}
\end{adjustwidth}

 \end{document} 

在此处输入图片描述

相关内容