我正在使用的包:\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
我的乳胶代码:
\begin{algorithm}[H]
\label{alg:my-alg}
\DontPrintSemicolon
\caption{My algorithm}
\SetKwInOut{Input}{Input}\SetKwInOut{Output}{Output}
\Input{$a,b,...$}
\Output{$R$}
statment1\;
statment2\;
statment3\;
\lIf{$a>b$}{\KwRet{$a$}}
\KwRet{$b$}\;
\end{algorithm}
我拥有的:
我想要的是:
答案1
使用tikzmark
库:
\documentclass{article}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
\begin{algorithm}[H]
\label{alg:my-alg}
\DontPrintSemicolon
\caption{My algorithm}
\SetKwInOut{Input}{Input}\SetKwInOut{Output}{Output}
\Input{$a,b,...$}
\Output{$R$}
statment1\;
statment2\;
statment3\;
\tikzmark{start}\lIf{$a>b$}{\KwRet{$a$\tikzmark{stop}}}
\KwRet{$b$}\;
\end{algorithm}
\begin{tikzpicture}[remember picture, overlay]
\draw[red,thick] ([yshift=0.5ex]pic cs:start) -- ([yshift=0.5ex]pic cs:stop);
\end{tikzpicture}
\end{document}