0

I tried to highlight parts of my pseudocode in algorithm2e with the following answer to a similar question: https://tex.stackexchange.com/a/51590/287916. I copied the code exactly the same and run it, but it doesn't compile with the following error: ! Package tikz Error: Cannot parse this coordinate.See the tikz package documentation for explanation.Type H for immediate help.... \end{frame}

Can anybody help me highlighting "if p<x" in one color and the corresponding "else" part in another one with a box around it?

\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}

Pseudocode to highlight

  • Welcome to this site. I edited the previous answer to put the frame behind the text. – pascal974 Jan 15 '23 at 07:14
  • I used Ulrike Fisher's answer in this post https://tex.stackexchange.com/questions/380436/background-colour-for-cells-with-padding/380440#380440 – pascal974 Jan 15 '23 at 07:22

1 Answers1

1

In the tikzmark documentation, we can read:

I wrote the original \tikzmark macro in 2009 for use in lecture slides prepared with the beamer package. Its original definition was:

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

And a little higher:

Using the \tikzmark is extremely simple. You need to load the tikz package and then load tikzmark as a tikzlibrary. Thus in your preamble you should have something like: \usepackage{tikz} \usetikzlibrary{tikzmark} In your document, you can now type \tikzmark{} at a point that you want to remember. This will save a mark with name for use later (or earlier). To use it in a \tikz or tikzpicture, simply use the pic coordinate system:

\tikz[remember picture] \draw[overlay] (0,0) -- (pic cs:<name>);

EDIT2: To have the colored frame behind the text, you must put the tikzpicture environment before the algorithm environment. It's sharper, the opacity is no longer useful. With the code:

    \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}

enter image description here

pascal974
  • 4,652