运行下面两段代码时,\usepackage 和 \newcommand 都会提示错误,如何解决?

运行下面两段代码时,\usepackage 和 \newcommand 都会提示错误,如何解决?
\documentclass{article}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[2]{%
  \begin{tikzpicture}[overlay,remember picture]
    \draw[->,shorten >=5pt,shorten <=5pt,out=70,in=130,distance=0.5cm,#1] (MarkA.north) to (MarkC.north);
    \draw[->,shorten >=5pt,shorten <=5pt,out=50,in=140,distance=0.3cm,#2] (MarkA.north) to (MarkB.north);
  \end{tikzpicture}
}
  
\begin{document}
\[
\dfrac{5}{6} \tikzmarknode{a}{\div \left(-\dfrac{3}{4}\right)}
    = \dfrac{5}{6} \tikzmarknode{b}{\times \left(-\dfrac{4}{3}\right)}
    = -\dfrac{10}{9}
%
    \begin{tikzpicture}[
overlay,remember picture,
transform canvas={yshift=-2pt}, cyan
                        ]
\draw [->] (a.south) -- ++ (0,-2mm) -| (b) node[pos=0.25, below] {text};
\draw   (a.south west) -- (a.south east)
        (b.south west) -- (b.south east);
    \end{tikzpicture}
\vspace{1.5\baselineskip}   % reserves space for the image
\]
\tikzmark{MarkA}$a$($b$\tikzmark{MarkB}+$c$\tikzmark{MarkC})\DrawBox{blue,distance=0.60cm,in=110,shorten >=3pt}{blue,out=60,in=110,distance=0.5cm}=$ab+ac$ 
    
\end{document}

答案1

在定义新的命令时,使用来自 LaTeX 或所用包的现有命令/环境名称。

如果我将您的定义更改\newcommand{\tikzmark} ..\newcommand{\TM},您的文档示例可以正常工作。但是,我不会定义您的新命令和环境,而是使用tikzmark库的选项。这样的代码更清晰,更不容易出错,而且在大多数情况下也更短。要比较两种编写示例的方式,请参见下面的 MWE:

\documentclass{article}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\newcommand{\TM}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[2]{%
  \begin{tikzpicture}[overlay,remember picture]
    \draw[->,shorten >=5pt,shorten <=5pt,out=70,in=130,distance=0.5cm,#1] (MarkA.north) to (MarkC.north);
    \draw[->,shorten >=5pt,shorten <=5pt,out=50,in=140,distance=0.3cm,#2] (MarkA.north) to (MarkB.north);
  \end{tikzpicture}
}

\begin{document}
\[
\dfrac{5}{6} \tikzmarknode{a}{\div \left(-\dfrac{3}{4}\right)}
    = \dfrac{5}{6} \tikzmarknode{b}{\times \left(-\dfrac{4}{3}\right)}
    = -\dfrac{10}{9}
%
    \begin{tikzpicture}[ overlay,remember picture,
                        transform canvas={yshift=-2pt}, cyan
                        ]
\draw [->] (a.south) -- ++ (0,-3mm) -| (b) node[pos=0.25, below] {text};
\draw   (a.south west) -- (a.south east)
        (b.south west) -- (b.south east);
    \end{tikzpicture}
\vspace{2\baselineskip}   % reserves space for the image
\]

\TM{MarkA}$a$($b$\TM{MarkB}+$c$\TM{MarkC})%
\DrawBox{blue,distance=0.60cm,in=110,shorten >=3pt}%
        {blue,out=60,in=110,distance=0.5cm}=$ab+ac$

\vspace{2\baselineskip}
$\tikzmarknode{A}{a} (\tikzmarknode{B}{b} + \tikzmarknode{C}{c}))$
    \begin{tikzpicture}[overlay,remember picture, cyan, looseness=1.5]
\draw [->] (A |- B.north) to[bend left=60] (B);
\draw [->] (A |- B.north) to[bend left=75] (C);    
    \end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容