TransWikia.com

TikZ arrows change direction when used with beamer, listings and itemize

TeX - LaTeX Asked by Anush on September 1, 2020

I asked this question on https://topanswers.xyz/tex?q=1244 but the question seems to have exposed a bug.

documentclass{beamer}
beamertemplatenavigationsymbolsempty
setbeamertemplate{frametitle}[default][center]
usepackage[many]{tcolorbox}
usepackage{listings}
lstdefinestyle{duckstyle}{%
    moredelim=[is][color{red}]{|}{|},
    mathescape=true,
    escapechar=@,
    basicstyle=ttfamily,
    columns=fullflexible
}
lstset{style=duckstyle}
newcommand{ubar}[1]{alt<+>{underaccent{bar}{#1}}{#1}}
tcbuselibrary{skins}
tcbset{
    arc=0pt,
    outer arc=0pt,
    colback=white,
}
usepackage{tikz}
usetikzlibrary{arrows.meta,
    shapes,
    tikzmark}
usetikzlibrary{tikzmark,shapes.geometric}
usetikzmarklibrary{listings} 

usepackage{calc}

newcommand{Proc}[1]{textsc{#1}}
newcommand{Var}[1]{ensuremath{textcolor{varcolor}{#1}}}
definecolor{varcolor}{RGB}{15,122,183}


definecolor{titlecolor}{RGB}{29, 110, 174}
begin{document}
begin{frame}[t, fragile]{}
Some text written here introducing the slide to the spectators.
begin{columns}

begin{column}{5.2cm} %

    begin{tcolorbox}[top=0pt, left=5pt,right=5pt, colback=blue!5!white, text width=5.2cm, text height=7cm]
begin{lstlisting}[mathescape, name=misra, basewidth = {.3em}]
Algorithm($Var{a_1,a_2,dots,a_m}$)

set $Var{A}$ = $emptyset$
For each $Var{i}$
end{lstlisting}
end{tcolorbox}
end{column}
    
   
begin{column}{textwidth-5cm} %
vspace{-0.8cm}
begin{center}
begin{tcolorbox}[top=0pt, left=5pt,right=5pt, colframe=blue, text width=4cm, text height=0.8cm]
end{tcolorbox}
end{center}
begin{itemize}[<+->] 
setlengthitemsep{5pt plus 1fill}
item[] $tikzmark{step1}A rightarrow tilde{f}_A = 1 $
item[] $C rightarrow tilde{f}_A = 1, tilde{f}_C = 1$
item[] $tikzmark{step2}A rightarrow tilde{f}_A = 2, tilde{f}_C = 1$
end{itemize}

end{column}
    begin{tikzpicture}[remember picture]
    only<1-2>{
    draw[->,overlay, dashed] (pic cs:step1) to [bend right]([xshift=0.2cm, yshift=.25baselineskip]pic cs:line-misra-3-end);
    }
    only<3>{
    draw[->,overlay, dashed] (pic cs:step2) to [bend right]([xshift=0.2cm, yshift=.25baselineskip]pic cs:line-misra-4-end);
    }
    end{tikzpicture}
end{columns}
end{frame} 
end{document}

The arrows should point from the right to the code on the left. But something happens after the first item to make them switch direction to point far off to the right.


An alternative solution using the tcolorbox version of listings can be found at https://topanswers.xyz/tex?q=1244

One Answer

The problem is that listings doesn't reset the line numbers and this confuses the tikzmarks (the problem with numbering is discussed here Problem with listings when using line numbers, `name`, and `beamer` overlays).

Add firstnumber=1 as option as a work-around:

documentclass{beamer}
beamertemplatenavigationsymbolsempty
setbeamertemplate{frametitle}[default][center]
usepackage[many]{tcolorbox}
usepackage{listings}
lstdefinestyle{duckstyle}{%
    moredelim=[is][color{red}]{|}{|},
    mathescape=true,
    escapechar=@,
    basicstyle=ttfamily,
    columns=fullflexible
}
lstset{style=duckstyle}
newcommand{ubar}[1]{alt<+>{underaccent{bar}{#1}}{#1}}
tcbuselibrary{skins}
tcbset{
    arc=0pt,
    outer arc=0pt,
    colback=white,
}
usepackage{tikz}
usetikzlibrary{arrows.meta,
    shapes,
    tikzmark}
usetikzlibrary{tikzmark,shapes.geometric}
usetikzmarklibrary{listings}

usepackage{calc}

newcommand{Proc}[1]{textsc{#1}}
newcommand{Var}[1]{ensuremath{textcolor{varcolor}{#1}}}
definecolor{varcolor}{RGB}{15,122,183}


definecolor{titlecolor}{RGB}{29, 110, 174}
begin{document}
begin{frame}[t, fragile]{}
Some text written here introducing the slide to the spectators.
begin{columns}

begin{column}{5.2cm} %

    begin{tcolorbox}[top=0pt, left=5pt,right=5pt, colback=blue!5!white, text width=5.2cm, text height=7cm]
begin{lstlisting}[firstnumber=1,mathescape, name=misra, basewidth = {.3em}]
Algorithm($Var{a_1,a_2,dots,a_m}$)

set $Var{A}$ = $emptyset$
For each $Var{i}$
end{lstlisting}
end{tcolorbox}
end{column}


begin{column}{textwidth-5cm} %
vspace{-0.8cm}
begin{center}
begin{tcolorbox}[top=0pt, left=5pt,right=5pt, colframe=blue, text width=4cm, text height=0.8cm]
end{tcolorbox}
end{center}
begin{itemize}[<+->]
setlengthitemsep{5pt plus 1fill}
item[] $tikzmark{step1}A rightarrow tilde{f}_A = 1 $
item[] $C rightarrow tilde{f}_A = 1, tilde{f}_C = 1$
item[] $tikzmark{step2}A rightarrow tilde{f}_A = 2, tilde{f}_C = 1$
end{itemize}

end{column}
    begin{tikzpicture}[remember picture]
    only<1-2>{
    draw[->,overlay, dashed] (pic cs:step1) to [bend right]([xshift=0.2cm, yshift=.25baselineskip]pic cs:line-misra-3-end);
    }
    only<3>{
    draw[->,overlay, dashed] (pic cs:step2) to [bend right]([xshift=0.2cm, yshift=.25baselineskip]pic cs:line-misra-4-end);
    }
    end{tikzpicture}
end{columns}
end{frame}
end{document}

Answered by Ulrike Fischer on September 1, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP