我想使用tcolorbox
包排版源代码。这个包如何不创建静态框,而是根据我编写的代码的宽度(垂直和水平)进行调整?另外,如果一行太长,我想换行。我可以这样做吗?
答案1
使用tcolorbox
及其listings
库,您可以用来\newtcblisting
定义列表的新环境(使用后台的listings
包);如果您使用hbox
的选项tcolorbox
,它的宽度将动态适应其内容的宽度,但是,唉!现在该框不允许分页符,并且自动换行符也得不到很好的支持(如果breaklines=true
作为列表的选项传递,代码将会中断,但现在该框对于长行不会表现得很好)。
\documentclass{article}
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\newtcblisting{mylisting}{
listing only,
hbox,
colframe=cyan,
colback=cyan!10,
listing options={
basicstyle=\small\ttfamily,
breaklines=true,
columns=fullflexible
},
}
\begin{document}
\begin{mylisting}
int main(int ac, char *av[])
{
printf("Hello, World");
return 0;
}
\end{mylisting}
\begin{mylisting}
int main(int ac, char *av[], char **ep,char *av[], char **ep)
{
printf("Hello, World");
return 0;
}
\end{mylisting}
\end{document}
那么,也许您可以默认使用带有自动换行符的可破坏框,并且hbox
如果需要短列表的可变宽度,则可以有选择地添加选项:
\documentclass{article}
\usepackage[a6paper]{geometry}% just for the example
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\newtcblisting{mylisting}[1][]{
listing only,
breakable,
colframe=cyan,
colback=cyan!10,
listing options={
language=C,
basicstyle=\small\ttfamily,
breaklines=true,
columns=fullflexible
},
#1
}
\begin{document}
\begin{mylisting}[hbox]
int main(int ac, char *av[])
{
printf("Hello, World");
return 0;
}
\end{mylisting}
\begin{mylisting}
int main(int ac, char *av[],int ac, char *av[],int ac, char *av[],int ac, char *av[],int ac, char *av[],int ac, char *av[])
{
printf("Hello, World");
return 0;
}
int main(int ac, char *av[])
{
printf("Hello, World");
return 0;
}
{
printf("Hello, World");
return 0;
}
{
printf("Hello, World");
return 0;
}
{
printf("Hello, World");
return 0;
}
{
printf("Hello, World");
return 0;
}
{
printf("Hello, World");
return 0;
}
\end{mylisting}
\end{document}
该tcolorbox
套餐还提供与minted
(另一个流行的列表包);然而,minted 不支持自动换行。使用现在的minted
库的一个小例子:
\documentclass{article}
\usepackage[many]{tcolorbox}
\tcbuselibrary{minted}
\newtcblisting{mylisting}{
colframe=cyan,
colback=cyan!10,
listing only,
listing engine=minted,
minted language=cpp,
minted options={fontsize=\small,linenos,numbersep=3mm},
}
\begin{document}
\begin{mylisting}
int main(int ac, char *av[])
{
printf("Hello, World");
return 0;
}
\end{mylisting}
\begin{mylisting}
int main(int ac, char *av[], char **ep,char *av[], char **ep)
{
printf("Hello, World");
return 0;
}
\end{mylisting}
\end{document}
答案2
虽然我不知道该怎么做tcolorbox
,但我展示了看似微不足道:verbatim、newenvironment 和 colorbox如何使用我的verbatimbox
软件包制作彩色逐字框。我尚未升级软件包以默认提供这些功能,因此它们包含在序言中。
请注意,这个答案确实不是支持自动换行。由于是方框形式,所以也不支持分页。
我修改了该答案(它提供了一个环境),提供了两个环境cverbbox
和mycverbbox
,后者允许您指定保存结果的框的唯一名称。我还在第 3 个示例中展示了如何使用可选参数对代码行进行编号。
\documentclass{article}
\usepackage{xcolor}
\usepackage{verbatimbox}
\makeatletter
\newenvironment{mycverbbox}[5][]{%
\setcounter{VerbboxLineNo}{0}%fx
\def\verbatim@processline{%
% THE FIRST #1 ACCOUNTS FOR NON-PRINTING COMMANDS; THE SECOND #1 IS FOR
% PRINTED OPTIONAL MATERIAL
{\addtocounter{VerbboxLineNo}{1}%
#1\setbox0=\hbox{#1\the\verbatim@line}%
\hsize=\wd0 \the\verbatim@line\par}}%
\@minipagetrue%
\@tempswatrue%
\global\edef\sv@name{\@macro@name{#5}}%
\global\edef\cverbboxColor{#3}%
\global\edef\cverbboxFColor{#4}%
\@ifundefined{\sv@name content}{%
\expandafter\newsavebox\expandafter{\csname\sv@name content\endcsname}%
}%
\expandafter\global\expandafter\edef\csname\sv@name\endcsname{\usebox{%
\csname\sv@name content\endcsname}}%
\setbox0=\vbox\bgroup\color{#2} \verbatim
}
{%
\endverbatim%
\unskip\setbox0=\lastbox %
\egroup%
\setbox1=\hbox{%
\colorbox{\cverbboxColor}{\box0}}%
\global\sbox{\csname\sv@name content\endcsname}%
{\fboxsep=\fboxrule\colorbox{\cverbboxFColor}{\box1}}%
}
\newenvironment{cverbbox}[4][]{%
\setcounter{VerbboxLineNo}{0}%fx
\def\verbatim@processline{%
% THE FIRST #1 ACCOUNTS FOR NON-PRINTING COMMANDS; THE SECOND #1 IS FOR
% PRINTED OPTIONAL MATERIAL
{\addtocounter{VerbboxLineNo}{1}%
#1\setbox0=\hbox{#1\the\verbatim@line}%
\hsize=\wd0 \the\verbatim@line\par}}%
\@minipagetrue%
\@tempswatrue%
\global\edef\sv@name{theverbbox}%
\global\edef\cverbboxColor{#3}%
\global\edef\cverbboxFColor{#4}%
\@ifundefined{\sv@name content}{%
\expandafter\newsavebox\expandafter{\csname\sv@name content\endcsname}%
}%
\expandafter\global\expandafter\edef\csname\sv@name\endcsname{\usebox{%
\csname\sv@name content\endcsname}}%
\setbox0=\vbox\bgroup\color{#2} \verbatim
}
{%
\endverbatim%
\unskip\setbox0=\lastbox %
\egroup%
\setbox1=\hbox{%
\colorbox{\cverbboxColor}{\box0}}%
\global\sbox{\csname\sv@name content\endcsname}%
{\fboxsep=\fboxrule\colorbox{\cverbboxFColor}{\box1}}%
}
\makeatother
\parskip 1ex
\begin{document}
\fboxrule=3pt\fboxsep=1pt
\begin{cverbbox}[\scriptsize\textcolor{black}{$\bullet$}]{red!80}{blue!10}{cyan}
here is \verbatim Text
Here
%$#@&^* \macros
xa
\end{cverbbox}
\fbox{An} inline verbatimbox\theverbbox is here.
\fboxrule=0pt\fboxsep=3pt
\begin{cverbbox}{blue}{cyan!40}{cyan}
here is frameless colorboxed \verbatim Text
by setting \fboxsep to 0pt.
No conditioning is performed with
an \optional argument
\end{cverbbox}
\theverbbox
\fboxrule=1pt\fboxsep=3pt\relax
\begin{mycverbbox}[\tiny\textcolor{black!50}{\tiny\makebox[4ex][r]{%
\theVerbboxLineNo:~}}]{black}{blue!10}{cyan}{\mycvbox}
subroutine cubic (p,q,r,rts)
c SOLVES CUBIC EQUATION x^3 + px^2 + qx + r = 0
c WITH REAL COEFFICIENTS. SOLUTION MAY BE COMPLEX.
implicit none
double precision p,q,r,rts(2,3)
double precision Ar, Ai, arg, ang, Br, Bi, SUMr, SUMi, DIFr,
& DIFi, a,b, radicand, term, PI
PARAMETER (PI=3.14159 26535 89793 23846 d0)
c
a = (3.d0*q - p**2)/3.d0
b = (2.d0*p**3 - 9.d0*p*q + 27.d0*r)/27.d0
radicand = b**2 /4.d0 + a**3 /27.d0
if (radicand .ge. 0.d0) then
term = -b/2.d0 + sqrt(radicand)
if (term .ge. 0.d0) then
Ar = term**(1.d0/3.d0)
else
Ar = -(-term)**(1.d0/3.d0)
end if
Ai = 0.d0
term = b/2.d0 + sqrt(radicand)
if (term .ge. 0.d0) then
Br = -(term**(1.d0/3.d0))
else
Br = (-term)**(1.d0/3.d0)
end if
Bi = 0.d0
else
arg = (sqrt((b/2.d0)**2 - radicand))**(1.d0/3.d0)
ang = atan(sqrt(-radicand)/(-b/2.d0))
if (ang .lt. 0.d0) ang = ang + PI
ang = ang/3.d0
Ar = arg*cos(ang)
Ai = arg*sin(ang)
Br = Ar
Bi = -Ai
end if
SUMr = Ar + Br
SUMi = Ai + Bi
DIFr = Ar - Br
DIFi = Ai - Bi
rts(1,1) = SUMr - p/3.d0
rts(2,1) = SUMi
rts(1,2) = -(SUMr - DIFi*sqrt(3.d0))/2.d0 - p/3.d0
rts(2,2) = -(SUMi + DIFr*sqrt(3.d0))/2.d0
rts(1,3) = -(SUMr + DIFi*sqrt(3.d0))/2.d0 - p/3.d0
rts(2,3) = -(SUMi - DIFr*sqrt(3.d0))/2.d0
return
end
c********************************************************************
\end{mycverbbox}
\mycvbox
\end{document}
\fboxrule
请注意,通过设置为可以获得无框框0pt
。