我有两个请求:
我正在尝试使用 并排比较两个代码tcolorbox
。问题是我无法将它们都放在代码格式中(IE,我已经在选项中指定了它们(一种是纯文本,另一种是代码)。comment and listing, comment = {#3},
但我不知道如何使用使它们彼此独立listing only
。
第二个要求是将两个代码垂直对齐到顶部(您可以看到右侧有一个额外的空行)。
先感谢您!
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage[most]{tcolorbox}
\lstdefinelanguage{myC++}{
breaklines=true,
breakatwhitespace=true,
language = c++
}
\lstdefinelanguage{myC++}{
breaklines=true,
breakatwhitespace=true,
language = c++
}
\lstset{language=myC++}
\newtcblisting[auto counter]{snippetbox}[3][]{
after=\par\nointerlineskip,
before=\par\nointerlineskip,
enhanced,
skin=bicolor,
lower separated=true,
fonttitle=\bfseries,
title=#2,
after title={\hfill Right code},
comment and listing,
comment = {#3},
listing options={aboveskip=0pt,
belowskip=0pt,
language=myC++},
sidebyside,
sidebyside align=top,
#1
}
\begin{document}
\begin{enumerate}
\item The first code snippet
\begin{snippetbox}{left code}
{int x = 0;//this should also be in code style.}
int x = 0;
\end{snippetbox}
\end{enumerate}
答案1
以下代码展示了并排显示两个列表的另一种方法。tcolorbox
它不使用一个列表,而是构建一个tcbraster
包含两个独立 tcblisting 的列表。
目前,代码构造不是最佳的,因为您必须使用空参数声明栅格和列表。我希望其他人可以做到这一点,但我不知道如何改进它。
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage[most]{tcolorbox}
\lstdefinelanguage{myC++}{
breaklines=true,
breakatwhitespace=true,
language = c++
}
\lstdefinelanguage{myC++}{
breaklines=true,
breakatwhitespace=true,
language = c++
}
\lstset{language=myC++}
\tcbset{
snippetraster/.style={
enhanced,
raster columns=2,
raster equal height=rows,
% raster before skip=0pt,
raster after skip=0pt,
raster left skip=0pt,
raster right skip=0pt,
raster column skip=0pt,
fonttitle=\bfseries,
listing only,
listing options={aboveskip=0pt, belowskip=0pt, language=myC++},
raster column 1/.style={
sharp corners=east,
title=Left code,
before title=\strut,
rightrule=0pt
},
raster column 2/.style={
colback=black!20,
sharp corners=west,
title=Right code,
before title=\strut\hfill,
leftrule=0pt
},
}
}
\begin{document}
\begin{enumerate}
\item The first code snippet
\begin{tcbraster}[snippetraster]
\begin{tcblisting}{} %<- Empty parameter needed (I don't know why)
int x = 0; //this should also be in code style.
\end{tcblisting}
\begin{tcblisting}{}
int x = 0;
\end{tcblisting}
\end{tcbraster}
\end{enumerate}
\end{document}