如何在 ieeetran 类中创建代码片段

如何在 ieeetran 类中创建代码片段

我希望实现的是代码周围有矩形,并且代码采用如图所示的字体格式。我应该如何实现这种格式?

代码片段示例代码

答案1

这里有一些方法(包括listingpercusse 已经展示的包):

\documentclass[journal,a4paper]{IEEEtran}
\usepackage{listings}
\usepackage{fancyvrb}
\usepackage{framed}
\usepackage[listings,skins]{tcolorbox}
\usepackage[skipbelow=\topskip,skipabove=\topskip]{mdframed}
\mdfsetup{roundcorner=0}

\begin{document}
\title{How to insert framed code}
\author{Me}
\maketitle
% The Abstract
\begin{abstract}
The article shows some methods to framed code.
\end{abstract}

% The Keywords 
\begin{IEEEkeywords}
Code, framed, boxes.
\end{IEEEkeywords}

\section{Framed code}

\subsection{Using listings}

\begin{lstlisting}[frame=single]
for i:=maxint to 0 do
begin
{ do nothing }
end;
\end{lstlisting}

\subsection{Using fancyvrb}

\begin{Verbatim}[frame=single]
for i:=maxint to 0 do
begin
{ do nothing }
end;
\end{Verbatim}

\subsection{Using framed+verbatim}

\begin{framed}
\begin{verbatim}
for i:=maxint to 0 do
begin
{ do nothing }
end;
\end{verbatim}
\end{framed}

\subsection{Using tcolorbox}
% setting for a simple frame
\tcbset{lowerbox=ignored,skin=freelance,frame code={
    \draw (frame.south west)rectangle(frame.north east);

  }
}

\begin{tcblisting}{}
for i:=maxint to 0 do
begin
{ do nothing }
end;
\end{tcblisting}

\subsection{Using mdframed+listings}

\begin{mdframed}
\begin{lstlisting}
for i:=maxint to 0 do
begin
{ do nothing }
end;
\end{lstlisting}
\end{mdframed}


\end{document}

在此处输入图片描述

答案2

您可以使用listings包装并根据您的需要进行调整。

\documentclass{ieeetran}
\usepackage{listings}
\usepackage{lipsum} %<- For dummy text

\title{The title}
\author{The author}
\begin{document}
\maketitle
\begin{abstract}
\lipsum[1]
\end{abstract}
\lipsum[1-4]

\begin{lstlisting}[frame=single,basicstyle=\ttfamily]
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
\end{lstlisting}

\end{document}

在此处输入图片描述

相关内容