Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In the following code, I would like to produce dashes lines instead of normal ones. The three HERE indicate where the code draws the lines.

\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} 
share|improve this question

1 Answer 1

up vote 6 down vote accepted

To do dashed rules you need to measure the box and then use leaders rather than a simple vrule, something like this

enter image description 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

\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} 
share|improve this answer
    
Thanks. I've forgotten to put on example with \usepackage[vlined,ruled]{algorithm2e}. In that case, I need to have also horizontal dashed rules : in the code, it's {\color{algoColorKeyword}\Hlne} that does the job. –  projetmbc Mar 31 '12 at 12:50
    
Oh that's easier actually, I updated my answer with a dashed \algocf@Hlne –  David Carlisle Mar 31 '12 at 14:31
    
That's great. Thanks for this ! –  projetmbc Mar 31 '12 at 14:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.