列表中的最终换行符以及标题中的换行符(使用 IEEEtran)

列表中的最终换行符以及标题中的换行符(使用 IEEEtran)

使用:

...我得到了以下 MWE:

% \documentclass[12pt,a4paper]{article}
\documentclass[12pt,a4paper,onecolumn]{IEEEtran}

\usepackage{listings}

\begin{document}

\noindent\begin{minipage}{.45\textwidth}
\begin{lstlisting}[basicstyle=\ttfamily,caption={[short]This is first line \\\hspace{\textwidth} This is second line},frame=tlrb]

function Something() {
  doOneThing() ;
  doAnotherThing() ;
  doThirdThing() ;
  if (something(wrong)) {
    repeat();
  }
}

\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}{.45\textwidth}
\begin{lstlisting}[basicstyle=\scriptsize\ttfamily,caption=code 2,frame=tlrb]{Name}

function SomethingElse() {
  doAnotherThing() ;
  doOneThing() ;
  doThirdThing() ;
  if (something(wrong)) {
    repeat();
  }
}

\end{lstlisting}
\end{minipage}

\end{document}

...这让我明白了:

测试.pdf

  • 我如何让清单 1 的多行标题正确对齐在列表框上方(请注意,这只是一个问题IEEEtran,而不是常规article类的问题)?
  • 如何在结尾列表(就像列表开头的单行空格被保留一样?)

答案1

  1. 使用 etoolbox 包我们在\@makecaption内部进行本地重新定义lstlisting,以纠正标题的垂直位置和间距。

  2. 用于showlines=true在列表末尾打印空行。

代码:

\documentclass[12pt,a4paper,onecolumn]{IEEEtran}
\usepackage{listings}
\usepackage{etoolbox}
\lstset{showlines=true}

\makeatletter
\AtBeginEnvironment{lstlisting}{%
\long\def\@makecaption#1#2{%
% test if is a for a figure or table
\ifx\@captype\@IEEEtablestring%
% if a table, do table caption
\footnotesize\bgroup\par\centering\@IEEEtabletopskipstrut{\normalfont\footnotesize #1}\\{\normalfont\footnotesize\scshape #2}\par\addvspace{0.5\baselineskip}\egroup%
\@IEEEtablecaptionsepspace
% if not a table, format it as a figure
\else
\@IEEEfigurecaptionsepspace
% 3/2001 use footnotesize, not small; use two nonbreaking spaces, not one
\setbox\@tempboxa\hbox{\normalfont\footnotesize {#1.}\nobreakspace\nobreakspace #2}%
\ifdim \wd\@tempboxa >\hsize%
% if caption is longer than a line, let it wrap around
\setbox\@tempboxa\hbox{\normalfont\footnotesize {#1.}\nobreakspace\nobreakspace}%
\parbox[b]{\hsize}{\normalfont\footnotesize\noindent\unhbox\@tempboxa#2}\medskip%
% if caption is shorter than a line, center if conference, left justify otherwise
\else%
\ifCLASSOPTIONconference \hbox to\hsize{\normalfont\footnotesize\hfil\box\@tempboxa\hfil}%
\else \hbox to\hsize{\normalfont\footnotesize\box\@tempboxa\hfil}\medskip%
\fi\fi\fi}}
\makeatother
\begin{document}

\noindent\begin{minipage}{.45\textwidth}
\begin{lstlisting}[basicstyle=\ttfamily,caption={[short]This is first line and some other text to span several lines},frame=tlrb]

function Something() {
  doOneThing() ;
  doAnotherThing() ;
  doThirdThing() ;
  if (something(wrong)) {
    repeat();
  }
}

\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}{.45\textwidth}
\begin{lstlisting}[basicstyle=\scriptsize\ttfamily,caption=code 2,frame=tlrb]{Name}

function SomethingElse() {
  doAnotherThing() ;
  doOneThing() ;
  doThirdThing() ;
  if (something(wrong)) {
    repeat();
  }
}


\end{lstlisting}
\end{minipage}

\end{document}

在此处输入图片描述

相关内容