我有 3 个问题。
- 如何才能使下方代码中心的低部分对齐并左对齐?
- 如何自动自定义maple下部的关键字?
- 如何自动缩进代码?
代码是:
\documentclass{article}
\usepackage{tcolorbox}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\tcbuselibrary{listings,skins,theorems}
\lstdefinelanguage{Maple}{
morekeywords={
and,assuming,break,by,catch,description,do,done,
elif,else,end,error,export,fi,finally,for,from,global,if,
implies,in,intersect,local,minus,mod,module,next,not,od,
option,options,or,proc,quit,read,return,save,stop,subset,then,
to,try,union,use,uses,while,xor
},
sensitive=true,
keywordstyle=\color{black},
morecomment=[l][\color{green!50!black}]\#,
morestring=[b]",
morestring=[d]"
aboveskip={0pt},
belowskip={0pt},
}[keywords,comments,strings]
\newtcblisting{code}[1]{
skin=bicolor,
colback=white,
colbacklower=white,
colupper=red!40!black,
collower=black,
listing options={language={Maple}},
fontupper=\ttfamily\bfseries,
fontlower=\itshape,
% fontlower=\rm\bf,
% math lower,
boxrule=0mm,
top=0mm,bottom=0mm,middle=0mm,
center lower,
% flushleft lower,
nobeforeafter,
listing and comment,
comment={ #1},
every listing line={\textcolor{red!40!black}{\ttfamily> }}
}
\begin{document}
\begin{code}{
Fib := proc(n::nonnegint)\\
option remember; system;\\
if $n < 2$ then $n$ else Fib$(n - 1)$ + Fib$(n - 2)$ end if\\
end proc
}
Fib := proc( n :: nonnegint )
option remember, system;
if n<2 then
n
else
Fib(n-1) + Fib(n-2)
end if;
end proc;
\end{code}
\end{document}
答案1
我不知道如何用 LaTeX 自动格式化和缩进伪代码。我非常确定无法tcolorbox
做到这一点 - 代码格式化是通过列表或minted
使用完成的tcolorbox
。伪代码可以使用以下包手动格式化算法2e,但不会自动执行。
为了获得居中的左对齐块,必须将内容放入随后居中的某个左对齐框中。
无需额外的包即可实现所需输出的一种方法是将伪代码放入表中,并使用简单的宏来格式化关键字,例如\mykw
:
\documentclass{article}
\usepackage{tcolorbox}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\tcbuselibrary{listings,skins,theorems}
\lstdefinelanguage{Maple}{
morekeywords={
and,assuming,break,by,catch,description,do,done,
elif,else,end,error,export,fi,finally,for,from,global,if,
implies,in,intersect,local,minus,mod,module,next,not,od,
option,options,or,proc,quit,read,return,save,stop,subset,then,
to,try,union,use,uses,while,xor
},
sensitive=true,
keywordstyle=\color{black},
morecomment=[l][\color{green!50!black}]\#,
morestring=[b]",
morestring=[d]"
aboveskip={0pt},
belowskip={0pt},
}[keywords,comments,strings]
\newtcblisting{code}[1]{
skin=bicolor,
colback=white,
colbacklower=white,
colupper=red!40!black,
collower=black,
listing options={language={Maple}},
fontupper=\ttfamily\bfseries,
fontlower=\itshape,
% fontlower=\rm\bf,
% math lower,
boxrule=0mm,
top=0mm,bottom=0mm,middle=0mm,
center lower,
% flushleft lower,
nobeforeafter,
listing and comment,
comment={ #1},
every listing line={\textcolor{red!40!black}{\ttfamily> }}
}
\begin{document}
\newcommand{\mykw}[1]{\textbf{\upshape #1}}
\begin{code}{
\begin{tabular}{l}
Fib := \mykw{proc}(n::nonnegint)\\
\mykw{option} remember; system;\\
\hspace{4mm}\mykw{if} $n < 2$ \mykw{then} $n$ \mykw{else} Fib$(n - 1)$ + Fib$(n - 2)$ \mykw{end if}\\
\mykw{end proc}
\end{tabular}
}
Fib := proc( n :: nonnegint )
option remember, system;
if n<2 then
n
else
Fib(n-1) + Fib(n-2)
end if;
end proc;
\end{code}
\end{document}
得出: