
Я немного играю сlstlistings
и мне интересно, будет ли это достижимо для всех моих объявлений:
Как добиться такого результата?
Примечание: Я ищу решение, которое можно применить ко всем моим спискам (например, определив новую команду) и которое автоматически сбалансирует два столбца...
решение1
Thelistings
пакет может прекрасно взаимодействовать с multicol
пакетом. Рамка сделана с помощью , tcolorbox
которая, в свою очередь, прекрасно сочетается с listings
. Кажется, она транзитивна!
Идея выделить цифры взята изОтвет Гонсало.
\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}
РЕДАКТИРОВАТЬКак и просили, версия с двумя ведущими нулями. К вашему сведению, \two@digits
это LaTeX
основной макрос (см. texdoc source2e
) и \three@digits
его не существует (поэтому я его определяю, но у вас снова могут возникнуть проблемы с \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}