5

I want the output generated by the listings and the algorithm package to be the same.

Consider the following minimum working example.

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

and its output

enter image description here

Both listings should be displayed exactly the same. The main requirement are the line numbers outside the box. Specifying colors for line numbers and boxes would be a bonus.

0

1 Answer 1

4

Here's one possibility (the code conatains explanatory comments; see also below):

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

enter image description here

Simply changing \numberfont you change the font attributes for numbers in both environments, changing \algnumbsep you control the separation between numbers and contents for both algorithms. Changing the definitions for the colors "Numbers" and "Frame", you can select the desired color for the numbers, and frame, respectively, in both environments. Changong \basicfont you simultaneously set the font for the text in algorithmic and for the basicstyle key in listings.

3
  • Very nice! Thank your very much. :) Two minor issues though: The line numbers are not exactly in the same style. For the second (algorithmic) listing, the line numbers are \ttfamily, if I am not mistaken. And looking at the second listing itself, the font does not seem to be monospaced; at least the characters are not aligned in fixed columns as in the first listing (same as in my min. working example).
    – Thomas
    Apr 26, 2013 at 0:55
  • @Thomas you were right about the numbers; now it's fixed. Regarding the columns=fixed option for listings, I am afraid that implementing it in algorithmicx will require a lot more work, and I'm sorry, but I don't feel like doing it. Apr 26, 2013 at 1:15
  • Thank you, Gonzalo! :) I really appreciate your work. And it is just that I don't understand why the second listing doesn't look like monospaced, fixed-column font. Didn't mean to ask you to implement it. Edit: With lmodern instead of bera the second listing actually looks "monospaced, fixed-column".
    – Thomas
    Apr 26, 2013 at 11:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .