对齐列表和算法包的外观

对齐列表和算法包的外观

我希望列表和算法包生成的输出相同。

考虑以下最小工作示例。

\documentclass[fontsize=12pt,a4paper,final,parskip=full]{scrreprt}

\usepackage{listings}
\usepackage{xcolor}

\definecolor{Numbers}{rgb}{0.3,0.3,0.3}
\definecolor{Frame}{rgb}{0.5,0.5,0.5}

\lstset{  
basicstyle=\small\ttfamily,
columns=fixed,
keywordstyle=\bfseries,       % keyword style
numbers=left,
frame=single,
xleftmargin=\parindent,
language={SPARQL},
numberstyle=\footnotesize\color{Numbers},
rulecolor=\color{Frame},
showspaces=false,
showstringspaces=false
}

\usepackage[boxed]{algorithm}
\usepackage[noend]{algpseudocode}

\begin{document}

\paragraph{First}\mbox{}

\begin{lstlisting}
SELECT *
WHERE {?s ?p ?o.}
\end{lstlisting}

\paragraph{Second}\mbox{}

\begin{algorithm}
\begin{algorithmic}[1]
\Function{doSomething}{params}
    \If{params == correct}
        \State   doIt
    \EndIf
\EndFunction            
\end{algorithmic}
\end{algorithm}

\end{document}

及其输出

在此处输入图片描述

两个列表应显示完全相同。主要要求是框外的行号。指定行号和框的颜色将是一个加分项。

答案1

这是一种可能性(代码包含解释性注释;另见下文):

\documentclass[fontsize=12pt,a4paper,final,parskip=full]{scrreprt}
\usepackage{listings}
\usepackage{xcolor}
\usepackage[boxed]{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{bera}% nice monospaced font with possibility for boldfaced characters

\definecolor{Numbers}{RGB}{200,34,34}
\definecolor{Frame}{RGB}{55,155,198}

% set distance between numbers and contents for both
% environments
\newlength\algnumbsep
\setlength\algnumbsep{5pt}
% set font properties for numbers in both environments
\newcommand\numberfont{\normalfont\footnotesize\color{Numbers}}
% set font properties for text in algorithmic and basic style for listings
\newcommand\basicfont{\small\ttfamily}

\lstset{  
basicstyle=\basicfont,
columns=fixed,
keywordstyle=\bfseries,       % keyword style
numbers=left,
numbersep=\algnumbsep,
frame=single,
xleftmargin=\parindent,
language={SPARQL},
numberstyle=\numberfont,
rulecolor=\color{Frame},
showspaces=false,
showstringspaces=false
}


% add color to numbers in algpseudocode
\algrenewcommand\alglinenumber[1]{\numberfont#1}%

\makeatletter
% add color to frame in algorithm "boxed" style
\renewcommand\fs@boxed{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@plain
  \def\@fs@pre{\setbox\@currbox\vbox{\hbadness10000
    \moveleft3.4pt\vbox{\advance\hsize by6.8pt
      \color{Frame}\hrule \hbox to\hsize{\vrule\kern3pt
        \vbox{\kern3pt\box\@currbox\kern3pt}\kern3pt\vrule}\hrule}}}%
  \def\@fs@mid{\kern2pt}%
  \def\@fs@post{}\let\@fs@iftopcapt\iffalse}
% add monospaced font, change \leftmargin and add control for number separation
% in the algorithmic environment
\renewenvironment{algorithmic}[1][0]%
   {\basicfont%
   \edef\ALG@numberfreq{#1}%
   \def\@currentlabel{\theALG@line}%
   %
   \setcounter{ALG@line}{0}%
   \setcounter{ALG@rem}{0}%
   %
   \let\\\algbreak%
   %
   \expandafter\edef\csname ALG@currentblock@\theALG@nested\endcsname{0}%
   \expandafter\let\csname ALG@currentlifetime@\theALG@nested\endcsname\relax%
   %
   \begin{list}%
      {\ALG@step}%
      {%
      \rightmargin\z@%
      \itemsep\z@ \itemindent\z@ \listparindent2em%
      \partopsep\z@ \parskip\z@ \parsep\z@%
      \labelsep\algnumbsep \topsep 0.2em%\skip 1.2em 
      \ifthenelse{\equal{#1}{0}}%
         {\labelwidth 0.5em}%
         {\labelwidth 1.2em}%
      \leftmargin0pt%\labelwidth \addtolength{\leftmargin}{\labelsep}% Ok. the perfect leftmargin :-))
      \ALG@tlm\z@%
      }%
   \setcounter{ALG@nested}{0}%
   \ALG@beginalgorithmic%
   }%
   {% end{algorithmic}
   % check if all blocks are closed
   \ALG@closeloops%
   \expandafter\ifnum\csname ALG@currentblock@\theALG@nested\endcsname=0\relax%
   \else%
      \PackageError{algorithmicx}{Some blocks are not closed!!!}{}%
   \fi%
   \ALG@endalgorithmic%
   \end{list}%
   }%
\makeatother
\begin{document}

\paragraph{First}\mbox{}

\begin{lstlisting}
SELECT *
WHERE {?s ?p ?o.}
\end{lstlisting}

\paragraph{Second}\mbox{}

\begin{algorithm}
\begin{algorithmic}[1]
\Function{doSomething}{params}
    \If{params == correct}
        \State   doIt
    \EndIf
\EndFunction            
\end{algorithmic}
\end{algorithm}

\end{document}

在此处输入图片描述

只需更改\numberfont即可更改两个环境中数字的字体属性,更改\algnumbsep即可控制两个算法中数字和内容之间的分离。更改“数字”和“框架”颜色的定义,您可以在两个环境中分别为数字和框架选择所需的颜色。使用 \basicfont,您可以同时设置 中的文本algorithmicbasicstyle中的键的字体listings

相关内容