在一个精美的框架内列出两列

在一个精美的框架内列出两列

我正在玩lstlistings我想知道这样的事情是否对于我的所有列表都可以实现:

清单

如何实现这种事情?

注意:我正在寻找一种可以应用于所有列表的解决方案(例如,通过定义一个新命令),并且可以自动平衡两列......

答案1

listings包可以与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}

在此处输入图片描述

相关内容