6

I am playing a little with lstlistings and I wonder whether such a thing would be achievable for all my listings:

listing

How to achieve this kind of things?

Note : I am searching for a solution I can apply to all my listings (by defining a new command for example), and which will automatically balance the two columns...

Vincent
  • 5,257

1 Answers1

13

The listings package can interact nicely with the multicol package. The frame stuff is done with the help of tcolorbox which, in turn, plays nicely with listings. It seems to be transitive!

The idea to highlight the numbers is taken from Gonzalo's answer.

enter image description here

\documentclass[landscape]{article}
\usepackage[margin = 2cm, includeheadfoot]{geometry}
\usepackage{lmodern}
\usepackage{multicol}
\usepackage{tcolorbox}
\tcbuselibrary{skins,listings}
\newtcblisting{mycpptwocol}[1][]{%
  enhanced,
  arc = 0pt,
  outer arc = 0pt,
  colback = blue!5,
  colframe = blue!50!black,
  listing only,
  fonttitle = \bfseries,
  listing options = {%
    language = C++,
    basicstyle = \ttfamily,
    multicols = 2,
    numbers = left,
    xleftmargin = 1em,
    showstringspaces = false,
  },
  overlay = {%
    \fill[gray!30] 
      (interior.north west)
      rectangle 
      ([xshift = 2em]interior.south west);
    \fill[gray!30] 
      ([xshift = -1em]interior.north)
      rectangle 
      ([xshift = 1em]interior.south);
    \draw[ultra thick, blue!50!black]
      ([xshift = -1em]interior.north) -- 
      ([xshift = -1em]interior.south); 
  },
  /utils/exec = {%
    \def\thelstnumber{%
      \texttt{\csname two@digits\endcsname{\the\value{lstnumber}}}}},
  title = {\centering\ttfamily #1}
}
\begin{document}

\begin{mycpptwocol}[helloworld.cpp]
#include <iostream>

int main()
{
    std::cout<<"Hello World``;
    return 0;
}
// This is a second column, just
// for fun !!!
\end{mycpptwocol}
\end{document}

EDIT As requested, a version with two leading zeros. FYI, \two@digits is a LaTeX core macro (see texdoc source2e) and \three@digits does not exist (so I define it, but you may again have some issue with \makeatletter).

\documentclass[landscape]{article}
\usepackage[margin = 2cm, includeheadfoot]{geometry}
\usepackage{lmodern}
\usepackage{multicol}
\usepackage{tcolorbox}
\tcbuselibrary{skins,listings}
\makeatletter
\def\three@digits#1{\ifnum#1<10 00\else\ifnum#1<100 0\fi\fi\number#1}
\makeatother
\newtcblisting{mycpptwocol}[1][]{%
  enhanced,
  arc = 0pt,
  outer arc = 0pt,
  colback = blue!5,
  colframe = blue!50!black,
  listing only,
  fonttitle = \bfseries,
  listing options = {%
    language = C++,
    basicstyle = \ttfamily,
    multicols = 2,
    numbers = left,
    xleftmargin = 1.5em,
    showstringspaces = false,
  },
  overlay = {%
    \fill[gray!30] 
      (interior.north west)
      rectangle 
      ([xshift = 2.5em]interior.south west);
    \fill[gray!30] 
      ([xshift = -1em]interior.north)
      rectangle 
      ([xshift = 1.5em]interior.south);
    \draw[ultra thick, blue!50!black]
      ([xshift = -1em]interior.north) -- 
      ([xshift = -1em]interior.south); 
  },
  /utils/exec = {%
    \def\thelstnumber{%
      \texttt{\csname three@digits\endcsname{\the\value{lstnumber}}}}},
  title = {\centering\ttfamily #1}
}
\begin{document}

\begin{mycpptwocol}[helloworld.cpp]
#include <iostream>

int main()
{
    std::cout<<"Hello World``;
    return 0;
}
// This is a second column, just
// for fun !!!
//
\end{mycpptwocol}
\end{document}

enter image description here

cjorssen
  • 10,032
  • 4
  • 36
  • 126
  • Great ! And how to be able to put the title as a parameter (because the title will be different for each listing) ? – Vincent May 11 '14 at 00:31
  • @Vincent Put \newtcblisting{mycpptwocol}[1][]{ and replace title=... with #1 in the definition of mycpptwocoland use \begin{mycpptwocol}[title = {\centering\ttfamily helloworld.cpp}] –  May 11 '14 at 00:50
  • One last question : In my code \makeatletter \renewcommand*\thelstnumber{% \texttt{\two@digits{\the\value{lstnumber}}}} \makeatletterfails with the messageIncomplete \iffalse; all text was ignored after line 1`. Any idea ? – Vincent May 11 '14 at 09:29
  • @Vincent See my edit (you should not have problem anymore). I also integrated Harish's idea (thanks). – cjorssen May 11 '14 at 09:43
  • @cjorssen And one last thing (and I validate your answer): I would like to have 3 digits of lstnumber and replacing \two by \three does not work... – Vincent May 11 '14 at 09:45
  • @Vincent See my edit. – cjorssen May 11 '14 at 10:38
  • @cjorssen Validated! Thanks for your amazing work! Following questions on alignments here : http://tex.stackexchange.com/questions/176533/aligning-and-centering-a-lstlisting-with-two-columns-on-a-page – Vincent May 11 '14 at 11:24