TikZ 箭头与 beamer、listings 和 itemize 一起使用时会改变方向

TikZ 箭头与 beamer、listings 和 itemize 一起使用时会改变方向

我问了这个问题https://topanswers.xyz/tex?q=1244但这个问题似乎暴露了一个错误。

\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}[<+->] 
\setlength\itemsep{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=.25\baselineskip]pic cs:line-misra-3-end);
    }
    \only<3>{
    \draw[->,overlay, dashed] (pic cs:step2) to [bend right]([xshift=0.2cm, yshift=.25\baselineskip]pic cs:line-misra-4-end);
    }
    \end{tikzpicture}
\end{columns}
\end{frame} 
\end{document}

箭头应该从右侧指向左侧的代码。但是在第一个箭头之后发生了一些事情,item导致它们改变方向,指向较远的右侧。


可以在以下位置找到使用 tcolorbox 版本列表的替代解决方案https://topanswers.xyz/tex?q=1244

答案1

问题是 listings 不会重置行号,这会混淆 tikzmarks(此处讨论了编号问题使用行号、“name”和“beamer”覆盖时出现列表问题)。

添加firstnumber=1选项作为解决方法:

\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}[<+->]
\setlength\itemsep{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=.25\baselineskip]pic cs:line-misra-3-end);
    }
    \only<3>{
    \draw[->,overlay, dashed] (pic cs:step2) to [bend right]([xshift=0.2cm, yshift=.25\baselineskip]pic cs:line-misra-4-end);
    }
    \end{tikzpicture}
\end{columns}
\end{frame}
\end{document}

相关内容