我想将我的算法限制在一个可以根据算法长度进行调整的框中,如下图所示
这是代码
\documentclass{article}
\usepackage[italian]{babel}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,top=3cm,bottom=3cm,left=1.5cm,right=1.5cm]{geometry}
\usepackage[italiano,linesnumbered,lined,boxruled,commentsnumbered]{algorithm2e}
\begin{document}
\def\AlCapFnt{\Large}
\def\AlCapNameFnt{\Large}
\SetAlFnt{\Large}
\begin{algorithm}
\caption{Somma unaria}\label{alg:Somma unaria}
\SetAlgoLined
\SetKwProg{Fn}{Funzione}{}{}
\Fn{int piu(int $n$, int $m$)}{return $m+n$.}
\SetKwProg{Def}{def}{:}{}
\SetKwFor{While}{while}{:}{}
\Def{piu($m,n$)}{\While{$n!=0$}{$m,n=m+1,n-1$} \textbf{return} $m$.}
\end{algorithm}
\end{document}
此外,是否存在一种方法可以为每种算法设置以下选项?
\def\AlCapFnt{\Large}
\def\AlCapNameFnt{\Large}
\SetAlFnt{\Large}
太感谢了
答案1
直接设置就可以了\AlFnt
,为了更清楚的显示效果,我使用颜色来代替字体大小来显示区别。
\documentclass{article}
\usepackage[italiano,linesnumbered,lined,boxruled,commentsnumbered]{algorithm2e}
\usepackage{xcolor}
\begin{document}
\begin{algorithm}
\SetAlCapNameFnt{\color{green}}
\SetAlCapFnt{\color{red}}
\color{yellow}
\caption{Somma unaria}
\SetAlgoLined
\SetKwProg{Fn}{Funzione}{}{}
\Fn{int piu(int $n$, int $m$)}{return $m+n$.}
\SetKwProg{Def}{def}{:}{}
\SetKwFor{While}{while}{:}{}
\Def{piu($m,n$)}{\While{$n!=0$}{$m,n=m+1,n-1$} \textbf{return} $m$.}
\end{algorithm}
\begin{algorithm}
\caption{Somma unaria}
\SetAlgoLined
\SetKwProg{Fn}{Funzione}{}{}
\Fn{int piu(int $n$, int $m$)}{return $m+n$.}
\SetKwProg{Def}{def}{:}{}
\SetKwFor{While}{while}{:}{}
\Def{piu($m,n$)}{\While{$n!=0$}{$m,n=m+1,n-1$} \textbf{return} $m$.}
\end{algorithm}
\end{document}