
다음에서 가져온 코드를 고려하십시오.여기멋진 목록을 작성하려면:
% 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열 텍스트 너비
- 베타: 첫 번째 열 너비
- 감마: 두 번째 열 너비
- 무: 숫자 열 너비
- 뉴: 숫자와 텍스트 사이의 거리
- 시그마:무+뉴
- 파이: 제목과 첫 번째 줄 사이의 거리
- psi: 마지막 줄 이후의 거리
멋져 보이지만 정렬이 잘못되어 두통이 발생합니다... 현재 결과 대신 다음을 원합니다.
- 베타 = 감마: 전체 열 너비가 동일합니다(따라서 가운데의 파란색 선이 페이지 중앙에 해당함).
- 무: 3자리 숫자를 포함하도록 조정합니다. (그리고 숫자가 3자리 길이인 경우 중앙에 오고 싶습니다.무)
- 파이 = psi: 위쪽과 아래쪽 여백이 동일함
논리를 정확히 알기 위해 다음을 사용하여 모든 것을 프로그래밍 방식으로 달성할 수 있다면 좋을 것입니다.
\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}