5

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...

  • Use a tabular. You will also need packages tblcolor and colorx if you want colors. – John Kormylo May 10 '14 at 15:45
  • I'd have a look at tcolorbox – cmhughes May 10 '14 at 15:51
  • You could also do it with Tikz or \fbox, \makebox and two minipages. I suppose you want a command to do all the work. – John Kormylo May 10 '14 at 15:54
11

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

  • 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 0:31
  • @Vincent Put \newtcblisting{mycpptwocol}[1][]{ and replace title=... with #1 in the definition of mycpptwocoland use \begin{mycpptwocol}[title = {\centering\ttfamily helloworld.cpp}] – user11232 May 11 '14 at 0:50
  • One last question : In my code \makeatletter \renewcommand*\thelstnumber{% \texttt{\two@digits{\the\value{lstnumber}}}} \makeatletter` fails with the message Incomplete \iffalse; all text was ignored after line 1. Any idea ? – Vincent May 11 '14 at 9:29
  • @Vincent See my edit (you should not have problem anymore). I also integrated Harish's idea (thanks). – cjorssen May 11 '14 at 9: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 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.