在下面的代码中,我想生成虚线而不是普通线条。三个HERE
表示代码绘制线条的位置。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage[ruled]{algorithm2e}
\definecolor{algoColorKeyword}{named}{blue}
\definecolor{algoColorComment}{named}{olive}
\renewcommand{\KwSty}[1]{{\color{algoColorKeyword}\textbf{#1}}}
\renewcommand{\CommentSty}[1]{{\color{algoColorComment}\texttt{#1}}}
\makeatletter
% The following lines come from algorithm2e.sty
%
% Block with a vertical line end by a little horizontal line
\renewcommand{\algocf@Vline}[1]{%
\strut\par\nointerlineskip%t
\algocf@push{\skiprule}%
\hbox{%
{\color{algoColorKeyword}\vrule}% HERE !
\vtop{%
\algocf@push{\skiptext}%
\vtop{\algocf@addskiptotal\advance\hsize by -\skiplength #1}%
{\color{algoColorKeyword}\Hlne}% HERE !
}%
}\vskip\skiphlne%
\algocf@pop{\skiprule}%
\nointerlineskip%
}
% The following lines come from algorithm2e.sty
%
% Block with a vertical line
\renewcommand{\algocf@Vsline}[1]{%
\strut\par\nointerlineskip%
\algocf@push{\skiprule}%
\hbox{%
{\color{algoColorKeyword}\vrule}% HERE !
\vtop{\algocf@push{\skiptext}%
\vtop{\algocf@addskiptotal\advance\hsize by -\skiplength #1}}%
}%
\algocf@pop{\skiprule}%
}
\makeatother
\begin{document}
\begin{algorithm}[H]
\tcp{Un exemple d'algorithme haut en couleur...}
\BlankLine
\KwData{Données au départ}
\KwResult{Ce que fait cet algorithme}
\BlankLine
\While{La boucle doit continuer}{
\eIf{Condition 1}{
\tcp{Un tout premier test...}
Action 1 \\
Action 2
}{
\For{$i = 2$ \KwTo $l$}{
Calcul $i$ \\
Action $i$
}
\tcp{
Un autre test \\
plus compliqué...
}
\uIf{Condition 2}{
Action $l + 1$ \\
Action $l + 2$
}
\ElseIf{Condition 3}{
Action $l + 3$ \\
Action $l + 4$
}
}
}
\caption{Un exemple d'algorithme}
\end{algorithm}
\end{document}
答案1
要执行虚线规则,您需要测量盒子,然后使用引线而不是简单的 vrule,如下所示
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage[ruled]{algorithm2e}
\definecolor{algoColorKeyword}{named}{blue}
\definecolor{algoColorComment}{named}{olive}
\renewcommand{\KwSty}[1]{{\color{algoColorKeyword}\textbf{#1}}}
\renewcommand{\CommentSty}[1]{{\color{algoColorComment}\texttt{#1}}}
\makeatletter
\def\algocf@Hlne{%
\hbox{\leaders \hbox{\vrule width 3pt height 0.6pt depth 0pt \hskip 2pt } \hskip 10pt}}
\def\mydashbox#1#2{%
\setbox0\hbox{#2}%
\dimen0\ht0
\advance\dimen0\dp0
\setbox2\vbox to \dimen0{{\color{#1}\leaders\vbox{\vskip2pt\hrule height 3pt width .6pt}\vfill}}%
\ht2=\ht0
\dp2=\dp0
\box2
\unhbox0
}
% The following lines come from algorithm2e.sty
%
% Block with a vertical line end by a little horizontal line
\renewcommand{\algocf@Vline}[1]{%
\strut\par\nointerlineskip%t
\algocf@push{\skiprule}%
\hbox{%
% {\color{algoColorKeyword}\vrule}% HERE !
\mydashbox{algoColorKeyword}{%
\vtop{%
\algocf@push{\skiptext}%
\vtop{\algocf@addskiptotal\advance\hsize by -\skiplength #1}%
{\color{algoColorKeyword}\Hlne}% HERE !
}%
}
}\vskip\skiphlne%
\algocf@pop{\skiprule}%
\nointerlineskip%
}
% The following lines come from algorithm2e.sty
%
% Block with a vertical line
\renewcommand{\algocf@Vsline}[1]{%
\strut\par\nointerlineskip%
\algocf@push{\skiprule}%
\hbox{%
% {\color{algoColorKeyword}\vrule}% HERE !
\mydashbox{algoColorKeyword}{%
\vtop{\algocf@push{\skiptext}%
\vtop{\algocf@addskiptotal\advance\hsize by -\skiplength #1}}%
}%
}
\algocf@pop{\skiprule}%
}
\makeatother
\begin{document}
\begin{algorithm}[H]
\tcp{Un exemple d'algorithme haut en couleur...}
\BlankLine
\KwData{Données au départ}
\KwResult{Ce que fait cet algorithme}
\BlankLine
\While{La boucle doit continuer}{
\eIf{Condition 1}{
\tcp{Un tout premier test...}
Action 1 \\
Action 2
}{
\For{$i = 2$ \KwTo $l$}{
Calcul $i$ \\
Action $i$
}
\tcp{
Un autre test \\
plus compliqué...
}
\uIf{Condition 2}{
Action $l + 1$ \\
Action $l + 2$
}
\ElseIf{Condition 3}{
Action $l + 3$ \\
Action $l + 4$
}
}
}
\caption{Un exemple d'algorithme}
\end{algorithm}
\end{document}