我尝试algorithm2e
用以下对类似问题的回答来突出显示我的伪代码的各部分:https://tex.stackexchange.com/a/51590/287916。我复制了完全相同的代码并运行它,但它无法编译并出现以下错误:!包 tikz 错误:无法解析此坐标。请参阅 tikz 包文档了解解释。输入 H 可立即获得帮助.... \end{frame}
有人能帮我用一种颜色突出显示“if p<x”并用另一种颜色突出显示相应的“else”部分并在其周围加一个框吗?
\usepackage[edges]{forest}
\usepackage{caption}
\usepackage[linesnumbered,noend]{algorithm2e}
\newcommand\mycommfont[1]{\tiny\ttfamily\textcolor{blue}{#1}}
\SetCommentSty{mycommfont}
\SetKw{Or}{or}
\SetKw{And}{and}
\SetKw{Break}{break}
\SetKw{True}{true}
\SetKw{False}{false}
\SetKw{myfor}{for loop}
\begin{document}
%\begin{frame}[fragile]
%\begin{forest}
% for tree={circle, draw,
% minimum size=1.5em,
% inner sep=1pt,
% align=center, top color=white
% },
% for leaves={rectangle},
% my label/.style={label={[font=\scriptsize]south:#1}},
% [, top color=gray, bottom color=gray, minimum size=4mm
% [E
% [ILIG, my label=3]
% [U
% [CH, my label=1]
% [LE, my label=5]]]
% [FLOG, my label=2]
% [GEL
% [BE, my label=7]
% [D, my label=8]]
% [MIT, my label=4]
% [ZU, my label=6]]
%\end{forest}
%\end{frame}
%
%\begin{frame}{TIKZ ARRAYBOX}
% \begin{tikzpicture}
% \pgftransparencygroup
% \nodes{p,a,t,t,e,r,n}
% \endpgftransparencygroup
% \pgfsetstrokeopacity{0.5}
% \pgfsetfillopacity{0.5}
% \pgftransparencygroup
% \nodes{?,?,?,?,?,?}
% \endpgftransparencygroup
% \pgfsetstrokeopacity{.75}
% \pgfsetfillopacity{.75}
% \pgftransparencygroup
% \brckt{1}{6}{0}{size=6}
% \endpgftransparencygroup
% \pgfsetfillopacity{0.5}
% \pgfsetstrokeopacity{0}
% \pgftransparencygroup
% \brckt{7}{12}{0}{free space}
% \endpgftransparencygroup
% \pgfsetstrokeopacity{0.5}
% \pgftransparencygroup
% \brckt{1}{12}{1}{capacity=12}
% \endpgftransparencygroup
% \end{tikzpicture}
%
%
% \begin{tikzpicture}[font=\ttfamily,
%array/.style={matrix of nodes,nodes={draw, minimum size=7mm, fill=green!30},column sep=-\pgflinewidth, row sep=0.5mm, nodes in empty cells,
%row 1/.style={nodes={draw=none, fill=none, minimum size=5mm}},
%row 1 column 1/.style={nodes={draw}}}]
%
%\matrix[array] (array) {
%0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9\\
% & & & & & & & & & \\};
%\node[draw, fill=gray, minimum size=4mm] at (array-2-9) (box) {};
%
%\begin{scope}[on background layer]
%\fill[green!10] (array-1-1.north west) rectangle (array-1-10.south east);
%\end{scope}
%
%\draw[<->]([yshift=-3mm]array-2-1.south west) -- node[below] {Array length is 10} ([yshift=-3mm]array-2-10.south east);
%
%\draw (array-1-1.north)--++(90:3mm) node [above] (first) {First index};
%\draw (array-1-10.east)--++(0:3mm) node [right]{Indices};
%\node [align=center, anchor=south] at (array-2-9.north west|-first.south) (8) {Element\\ (at index 8)};
%\draw (8)--(box);
%%
%\end{tikzpicture}
%\end{frame}
\begin{frame}
\scalebox{0.5}{
\begin{algorithm}[H]
\SetInd{0.3em}{0.8em}
\DontPrintSemicolon
\KwIn{Compact Suffix Trie T für Text X, Pattern P}
\KwOut{Startindex eines Matchs in X oder "KEIN MATCH"}
$p \gets P.length()$ \;
$j \gets 0$ \;
$v \gets T.root()$\;
\Repeat{failed \Or T.isExternal(v)}{
$failed \gets$ \True \;
\ForEach{Kindknoten w von v}{
$i \gets start(w)$\;
\If{P[j] = X[i]}
{
$x \gets end(w)-i+1$\;
\eIf(\tcp*[h]{HIGHLIGHT FROM HERE IN ONE COLOR}){$p \leq x$}{
\eIf{$P[j..j+p-1] = X[i..i+p-1]$}{
\Return{i-j}
}{
\Return{"KEIN MATCH"}
}
}(\tcp*[h]{HIGHLIGHT ELSE IN ANOTHER COLOR})
{
\If{$P[j..j+x-1] = X[start(w)..end(w)]$}{
$p \gets p-x$\;
$j \gets j+x$\;
$v \gets w$\;
$failed \gets \False$\;
\Break \myfor \;
}
}
}
}
}
\Return{"KEIN MATCH"}
\end{algorithm}}
\end{frame}
\end{document}
答案1
在 tikzmark 文档中,我们可以读到:
我于 2009 年编写了原始 \tikzmark 宏,用于使用 beamer 包准备的讲座幻灯片。其原始定义是:
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
还有稍微高一点的:
使用 \tikzmark 非常简单。您需要加载 tikz 包,然后将 tikzmark 作为 tikzlibrary 加载。因此,在您的序言中,您应该有类似以下内容:\usepackage{tikz} \usetikzlibrary{tikzmark} 在您的文档中,您现在可以在想要记住的位置输入 \tikzmark{}。这将保存一个带有名称的标记以供以后(或更早)使用。要在 \tikz 或 tikzpicture 中使用它,只需使用 pic 坐标系:
\tikz[remember picture] \draw[overlay] (0,0) -- (pic cs:<name>);
编辑 2:要让彩色框架位于文本后面,必须将环境放在环境tikzpicture
之前algorithm
。它更清晰,不透明度不再有用。使用代码:
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{calc,tikzmark}
\usepackage[linesnumbered,noend]{algorithm2e}
\newcommand\mycommfont[1]{\tiny\ttfamily\textcolor{blue}{#1}}
\SetCommentSty{mycommfont}
\SetKw{Or}{or}
\SetKw{And}{and}
\SetKw{Break}{break}
\SetKw{True}{true}
\SetKw{False}{false}
\SetKw{myfor}{for loop}
\begin{document}
\begin{frame}
\scalebox{0.5}{
%%%%%%%%%%%%%%%%%%%%%%%%% before the algo
\begin{tikzpicture}[remember picture,overlay]
\draw [fill=yellow] ($(pic cs:a) + (-0.2,0.3)$) rectangle ($(pic cs:b)+(7,-0.1)$);
\draw [fill=blue!50] ($(pic cs:c) + (-0.2,0.3)$) rectangle ($(pic cs:d)+(5.,-0.1)$);
\end{tikzpicture}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{algorithm}[H]
\SetInd{0.3em}{0.8em}
\DontPrintSemicolon
\KwIn{Compact Suffix Trie T für Text X, Pattern P}
\KwOut{Startindex eines Matchs in X oder "KEIN MATCH"}
$p \gets P.length()$\;
$j \gets 0$ \;
$v \gets T.root()$\;
\Repeat{failed \Or T.isExternal(v)}{
$failed \gets$ \True \;
\ForEach{Kindknoten w von v}{
$i \gets start(w)$\;
\If{P[j] = X[i]}
{
$x \gets end(w)-i+1$\;
\eIf(\tcp*[h]{HIGHLIGHT FROM HERE IN ONE COLOR}){$p \leq x$}{
\tikzmark{a}\eIf{$P[j..j+p-1] = X[i..i+p-1]$}{
\Return{i-j}
}{
\Return{"KEIN MATCH"}
}\tikzmark{b}
}(\tcp*[h]{HIGHLIGHT ELSE IN ANOTHER COLOR})
{
\tikzmark{c}\If{$P[j..j+x-1] = X[start(w)..end(w)]$}{
$p \gets p-x$\;
$j \gets j+x$\;
$v \gets w$\;
$failed \gets \False$\;
\Break \myfor \tikzmark{d}\;
}
}
}
}
}
\Return{"KEIN MATCH"}
%%%%%%%%%%%%%%%%%%%%%%%% MODIFIED
% \begin{tikzpicture}[remember picture,overlay]
% \draw [fill=yellow,opacity=0.5] ($(pic cs:a) + (-0.2,0.3)$) rectangle ($(pic cs:b)+(7,-0.1)$);
% \draw [fill=blue,opacity=0.5] ($(pic cs:c) + (-0.2,0.3)$) rectangle ($(pic cs:d)+(5.,-0.1)$);
% \end{tikzpicture}
\end{algorithm}
}
\end{frame}
\end{document}