ページに2列のリストを配置して中央揃えする

ページに2列のリストを配置して中央揃えする

以下のコードを検討してください。ここ魅力的なリストを作成するには:

% PACKAGES
\documentclass[11pt,a4paper]{book}
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage{lmodern}
\usepackage{multicol}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{tcolorbox}
\tcbuselibrary{skins,listings}
\definecolor{nicedarkgreen}{RGB}{0,127,0}
\definecolor{nicedarkblue}{RGB}{0, 0, 102}

% LISTINGS
\newtcblisting{cpplisting}[1][]{%
  enhanced,
  arc = 0pt,
  outer arc = 0pt,
  colback = blue!5,
  colframe = nicedarkblue,
  listing only,
  fonttitle = \bfseries,
  listing options = {%
    basicstyle=\tiny\ttfamily,
    keywordstyle=\color{blue},
    language = C++,
    commentstyle=\color{nicedarkgreen},
    tabsize=2,
    morekeywords={constexpr},
    multicols = 2,
    numbers = left,
    xleftmargin = 0.5em,
    showstringspaces = false,
  },
  overlay = {%
    \fill[gray!30] 
      (interior.north west)
      rectangle 
      ([xshift = 1.25em]interior.south west);
    \fill[gray!30] 
      ([xshift = -1em]interior.north)
      rectangle 
      ([xshift = 0.25em]interior.south);
    \draw[ultra thick, nicedarkblue]
      ([xshift = -1em]interior.north) -- 
      ([xshift = -1em]interior.south); 
  }
  /utils/exec = {%
    \def\thelstnumber{%
      \texttt{\csname two@digits\endcsname{\the\value{lstnumber}}}}},
  title = {\centering\ttfamily #1}
}

% TEST
\begin{document}
\lipsum[1]
\begin{cpplisting}[An example of C++ file]
// Include
#include <iostream>

// Example
int main()
{
    std::cout<<"Hello World"<<std::endl;
    std::cout<<"This is just an example"<<std::endl;
    std::cout<<"With two columns"<<std::endl;
    return 0;
}
\end{cpplisting}
\end{document}

リスト結果

と :

  • アルファ: 1列テキスト幅
  • ベータ: 最初の列の幅
  • ガンマ: 2列目の幅
  • ミュー: 数値列幅
  • ニュー: 数字とテキスト間の距離
  • シグマ:む+ぬ
  • ファイ: タイトルと最初の行の間の距離
  • サイ: 最後の行からの距離

見た目は素晴らしいのですが、位置合わせがうまくいっていないため、頭が痛くなります... 現在の結果の代わりに、次の結果が欲しいです:

  • ベータ = ガンマ: 列の合計幅は同じです(中央の青い線がページの中央に一致します)
  • ミュー: 3桁になるように調整します(数字が3桁の場合は、中央に配置します)。ミュー
  • ファイ = サイ: 上下の余白が同じ

ロジックを正確に知るには、以下を使用してすべてをプログラムで実現できれば素晴らしいでしょう。

\newlength{...}
\setlength{...}{...}

または同様のもの。

これを実現するにはどうすればいいでしょうか?

答え1

3 時間以上もためらいながら試行錯誤した結果がこれです。特定のフォント/フォント サイズで希望どおりの結果が出ていますが、まだ実験段階です。異なるフォント/フォント サイズに合わせて適切にスケーリングできるように改善していただければ幸いです。

% PACKAGES
\documentclass[11pt,a4paper]{book}
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage{lmodern}
\usepackage{multicol}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{tcolorbox}
\usepackage{layouts}
\usepackage{calc}
\tcbuselibrary{skins,listings}
\definecolor{nicedarkgreen}{RGB}{0,127,0}
\definecolor{nicedarkblue}{RGB}{0, 0, 102}
\definecolor{lstgrey}{RGB}{217, 217, 217}
\definecolor{lstback}{RGB}{242, 242, 255}

% LISTINGS
\newlength{\zerolength}
\setlength{\zerolength}{0em}
\newlength{\tinyheight}
\setlength{\tinyheight}{\totalheightof{\tiny\ttfamily PpQq}}
\newlength{\beforetitle}
\setlength{\beforetitle}{2pt}
\newlength{\numberlength}
\setlength{\numberlength}{\widthof{\tiny\ttfamily 9999}}
\setlength{\numberlength}{0.25\numberlength}
\makeatletter
\def\three@digits#1{\ifnum#1<10 00\else\ifnum#1<100 0\fi\fi\number#1}
\makeatother
\newtcblisting{cpplisting}[1][]{%
  % Frame
  enhanced,
  arc = \zerolength,
  outer arc = \zerolength,
  % Title
  fonttitle = \ttfamily,
  toptitle = \zerolength,
  bottomtitle = \zerolength,
  titlerule = \zerolength,
  toptitle = \beforetitle,
  bottomtitle = \beforetitle,
  % Vertical alignment
  top = -0.5\tinyheight,
  bottom = 0.25\tinyheight,
  % Horizontal alignment
  left = \zerolength,
  right = 1.5\numberlength,
  colback = lstback,
  colframe = nicedarkblue,
  % Contents
  listing only,
  title = {\centering #1},
  listing options = {%
    % Language
    language = C++,
    tabsize=2, 
    basicstyle=\tiny\ttfamily,
    keywordstyle=\color{blue},
    commentstyle=\color{nicedarkgreen},
    morekeywords={constexpr},
    showstringspaces = false,
    % Alignment
    multicols = 2,
    numbers = left,
    numbersep = 2\numberlength, 
    xleftmargin = 5\numberlength,
    framesep = \zerolength,
    aboveskip = \zerolength,
    belowskip = \zerolength,
  },
  overlay = {%
    \fill[lstgrey] (interior.north west) rectangle ([xshift = 5\numberlength] interior.south west);
    \draw[ultra thick, nicedarkblue] ([xshift = \zerolength] interior.north) -- ([xshift = \zerolength] interior.south);
    \fill[lstgrey] (interior.north) rectangle ([xshift = 5\numberlength] interior.south); 
  },
  %/utils/exec = {%
  %  \def\thelstnumber{%
  %    \texttt{\csname three@digits\endcsname{\the\value{lstnumber}}}}}
}

% TEST
\begin{document}
\lipsum[1]
\begin{cpplisting}[An example of a C++ file]
// Include
#include <iostream>

// Example
int main()
{
    std::cout<<"Hello World"<<std::endl;
    std::cout<<"This is just an example"<<std::endl;
    std::cout<<"With two columns"<<std::endl;
    return 0;
}
\end{cpplisting}
\end{document}

リスト結果

関連情報