tikpicture 每次编辑都会移动位置

tikpicture 每次编辑都会移动位置

我正在使用 TikZ 绘制箭头或定位图形。我的代码有三张幻灯片。TikZ 在前两张幻灯片上工作正常,但在最后一张幻灯片上,图像会随着每次编译而移动。我怀疑原因在于第一张幻灯片中的设置(remember picture)。但是我无法修复它。

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{calc,tikzmark,arrows,shapes,backgrounds}
\usepackage{xparse}%  For \NewDocumentCommand
\newcommand{\mytikzmark}[1]{\tikz[overlay,remember picture] \node[baseline] (#1) {};}
\begin{document}

% For every picture that defines or uses external nodes, you'll have to
% apply the 'remember picture' style. To avoid some typing, we'll apply
% the style to all pictures.
\tikzstyle{every picture}+=[remember picture]
\tikzstyle{na} = [baseline=-.5ex]

\begin{frame}{Polynomials}
\begin{itemize}
\item Many functions can be well described by a high-order
polynomial
\item MATLAB represents a polynomials by a vector of coefficients
\begin{itemize} \item if vector P describes a polynomial\end{itemize}
\begin{figure}
\begin{tikzpicture}
%\draw[help lines](-2,-1)grid(4,2); % shows background
\coordinate (origin) at (0,0);
%\node at (origin) {*};
\coordinate  (text1) at (1,1);
\node at (text1) {
$
\mytikzmark{B1}{a}\mytikzmark{B2}{x^3}+\mytikzmark{B3}{b}\mytikzmark{B4}{x^2}+\mytikzmark{B5}{c}\mytikzmark{B6}{x+}\mytikzmark{B7}{d}\mytikzmark{B8}{   }
$
};
\node [text width=1.0cm, anchor=east] at (0, 0) (P1) {\textcolor{blue}{P(1)}};
\draw[blue,-stealth, thick, shorten >= 0.5ex, shorten <= -0.5ex] (P1) -- ($(B1)!0.5!(B2)$);

\node [text width=1.0cm, anchor=east] at (1, 0) (P2) {\textcolor{blue}{P(2)}};
\draw[blue,-stealth, thick, shorten >= 0.5ex, shorten <= -0.5ex] (P2) -- ($(B3)!0.5!(B4)$);

\node [text width=1.0cm, anchor=east] at (2.5, 0) (P3) {\textcolor{blue}{P(3)}};
\draw[blue,-stealth, thick, shorten >= 0.5ex, shorten <= -0.5ex] (P3) -- ($(B5)!0.5!(B6)$);

\node [text width=1.0cm, anchor=east] at (3.5, 0) (P4) {\textcolor{blue}{P(4)}};
\draw[blue,-stealth, thick, shorten >= 0.5ex, shorten <= -0.5ex] (P4) -- ($(B7)!0.5!(B8)$);

\end{tikzpicture}
\end{figure}

\item P=[1 0 -2] represents the polynomial $x^2-2$
\item P=[2 0 0 0] represents the polynomial $2x^3$
\end{itemize}
\end{frame}
\begin{frame}{Nonlinear Root Finding}
\begin{columns}[b]
\begin{column}{0.7\textwidth}
\begin{itemize}
\item Many real-world problems require us to solve f(x)=0
\item Can use {\textbf{\textcolor{orange}{fzero}}} to calculate roots for any arbitrary function
\item {\textbf{\textcolor{orange}{fzero}}} needs a function passed to it.
\item We will see this more and more as we delve into solving
equations.
\item Make a separate function file
\newline
{\scriptsize{\fontseries{b}\texttt{
\textcolor{blue}{
>> x=fzero('myfun',1);\tikz[na] \coordinate (s-mat); \\
>> x=fzero(@myfun,1)}}}}
\begin{itemize} \item 1 specifies a
point close to where
you think the root is\end{itemize}
\end{itemize}
\vspace{0pt}
\end{column}
\begin{column}{0.4\linewidth}
\begin{tikzpicture}[remember picture,overlay]
\node[anchor=south west,inner sep=0pt] (A) at ($(current page.south west)+(7.4cm,2.1cm)$)
{\includegraphics[width=5.4cm]{image2}};
\path (A) coordinate (mat);         
\end{tikzpicture}
\vspace{0pt}
\end{column}
\end{columns}
\begin{tikzpicture}[overlay]
\path[->,black,thick] (s-mat) edge (A);
\end{tikzpicture}

\end{frame}
\begin{frame}{Anonymous Functions}
\begin{itemize}
\item You do not have to make a separate function file
\newline
{\scriptsize{\fontseries{b}\texttt{
\textcolor{blue}{
>> x=fzero(@myfun,1)}}}}
\begin{itemize}\item What if myfun is really simple?\end{itemize}
\item Instead, you can make an anonymous function
\end{itemize}
\begin{tikzpicture}
\draw[help lines](-2,-1)grid(4,2); % shows background
\coordinate (origin) at (0,0);
\node at (origin) {*};
\coordinate  (text1) at (0,1);
\coordinate (text2) at (2,2);
\node [text width=8.0cm] at (text1) {
{\scriptsize{\fontseries{b}\texttt{
\textcolor{blue}{
>> x=fzero(@\mytikzmark{B1}{(}x\mytikzmark{B2}{)}\textcolor{black}{$\underbrace{\text{\textcolor{blue}{\mytikzmark{B3}{(}cos(exp(x))+x\textasciicircum 2-1\mytikzmark{B4}{)}}}}$}, 1 );}}}}
};
\node  [text width=8.0cm]  at (text2) {
{\scriptsize{\fontseries{b}\texttt{
\textcolor{blue}{>> x=fminbnd(@(x) (cos(exp(x))+x\textasciicircum2-1),-1,2);}}}}
};
\node [text width=1.0cm, anchor=east] at (0, 0) (P1) {\textcolor{blue}{P(1)}};
\draw[blue,-stealth, thick, shorten >= 0.5ex, shorten <= -0.5ex] (P1) -- ($(B1)!0.5!(B2)$);

\node [text width=1.0cm, anchor=east] at (1, 0) (P2) {\textcolor{blue}{P(2)}};
\draw[blue,-stealth, thick, shorten >= 0.5ex, shorten <= -0.5ex] (P2) -- ($(B3)!0.5!(B4)$);
\end{tikzpicture}
\end{frame}
\end{document} 

这就是我想要的最后一张幻灯片所要表达的内容。 在此处输入图片描述

答案1

在此处输入图片描述

代码:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{calc,tikzmark,arrows,shapes,backgrounds,decorations.pathreplacing}
\usepackage{xparse}%  For \NewDocumentCommand

% For every picture that defines or uses external nodes, you'll have to
% apply the 'remember picture' style. To avoid some typing, we'll apply
% the style to all pictures.
\tikzset{
  every picture/.append style={remember picture},
  na/.style={baseline=-.5ex},
  arrow/.style={-stealth}
}  

\newcommand\MyFont[1]{%
  {\scriptsize\bfseries\texttt{\textcolor{blue}{#1}}}%
}

\begin{document}

\begin{frame}{Anonymous Functions}
\begin{itemize}
\item You do not have to make a separate function file

\MyFont{>> x=fzero(@myfun,1)}

  \begin{itemize}\item What if myfun is really simple?\end{itemize}
  \item Instead, you can make an anonymous function

  \MyFont{
    >> x=fzero(@(\tikzmark{B1}x)
    \tikzmark{B3}(cos(exp(x))+x\textasciicircum 2-1\tikzmark{B4}), 1 )}
    \vspace{0.8cm}

  \MyFont{>> x=fminbnd(@(x) (cos(exp(x))+x\textasciicircum2-1),-1,2);}
  \end{itemize}

\begin{tikzpicture}[remember picture,overlay]
\draw[arrow] 
  ( $ (pic cs:B1) + (-10pt,-0.3cm) $ ) node[below,font=\scriptsize] {input} -- 
  ( $ (pic cs:B1) + (0.4ex,-2pt) $ );
\draw[decorate,decoration={brace,mirror,raise=4pt}] 
  (pic cs:B3) -- (pic cs:B4) node[midway,below=9pt,font=\scriptsize] {function to evaluate};
\end{tikzpicture}
\end{frame}

\end{document}

评论

  • 我对您的代码做了一些修改。特别是,我简化了它,定义了一个可以轻松更改字体的命令,并删除了多余的元素。

  • 在您的代码中,您误用了\mytikzmark;您用一个参数定义它,但您在使用它时,因为它有两个参数: ;这是不正确的,并且在和\mytikzmark{B3}{(}之间产生了奇怪的交互。\text\mytikzmark

  • 由于您已在加载它,我建议您使用tikzmark库并让它的\tikzmark命令为您完成艰苦的工作,就像我在示例代码中所做的那样。

  • 查看您的代码,似乎您也在\mytikzmark另一个意义上滥用了该命令;该命令的主要思想是为元素放置一些标记,然后在tikzpicture 外部它们的定义位置。您将标记放置于同一个 中并使用它们tikzpicture,因此这确实有点矫枉过正(除了嵌套tikzpictures 之外)。在我的代码中,我将标记放置与使用分开。

  • 显然蓝色文本代表代码,所以我建议你使用listings包以方便编写。matlab-prettifier您也可能对该包感兴趣。

相关内容