【问题标题】:How can I fix this error when using Tikz for Overleaf?将 Tikz 用于 Overleaf 时如何解决此错误?
【发布时间】:2020-05-01 14:54:24
【问题描述】:

我正在尝试用箭头绘制图表。我已经能够使用此代码生成图表但没有箭头:

\begin{tikzpicture}

\node (a) at (0,0) {$a$};    
\node (z1) at (3,2) {$z_1$};    
\node (z2) at (3,0) {$z_2$};    
\node (z3) at (3,-2) {$z_3$};


\draw (a) -- (z1);    
\draw (a) -- (z2);    
\draw (a) -- (z3);

\end{tikzpicture}

但我需要将段作为箭头并向段添加标签但是当我尝试这段代码时:

\begin{tikzpicture}

\node (a) at (0,0) {$a$};    
\node (z1) at (3,2) {$z_1$};    
\node (z2) at (3,0) {$z_2$};    
\node (z3) at (3,-2) {$z_3$};

\draw [->] (a) -- (z1) {$1/2$};    
\draw [->] (a) -- (z2) {$1/4$};    
\draw [->] (a) -- (z3) {$1/4$};

\end{tikzpicture}

我在第一行 \draw 中收到错误消息:

Argument of \language?@active@arg> has an extra }. 


Runaway argument? 

Missing \endcsname inserted.

然后在\end{tikzpicture} 行我得到:

Extra }, or forgotten \endgroup.

Missing } inserted. 

我使用的完整代码是这样的:

\documentclass[spanish]{article}
\usepackage[utf8]{inputenc}

\usepackage[spanish]{babel}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr} % Required for custom headers
\usepackage{lastpage} % Required to determine the last page for the footer
\usepackage{extramarks} % Required for headers and footers
\usepackage[usenames,dvipsnames]{color} % Required for custom colors
\usepackage{graphicx} % Required to insert images
\usepackage{listings} % Required for insertion of code
\usepackage{courier} % Required for the courier font
\usepackage{lipsum} % Used for inserting dummy 'Lorem ipsum' text into the template
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{natbib}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\node (a) at (0,0) {$a$};    
\node (z1) at (3,2) {$z_1$};    
\node (z2) at (3,0) {$z_2$};    
\node (z3) at (3,-2) {$z_3$};

\draw [->] (a) -- (z1)  node[midway,above] {$1/2$};    
\draw [->] (a) -- (z2)  node[midway,above] {$1/4$};    
\draw [->] (a) -- (z3)  node[midway,above] {$1/4$};

\end{tikzpicture}

\end{document}

【问题讨论】:

  • 语法错误。 \draw 没有标签。您希望文本1/2 出现在哪里?您是否正在寻找edge 命令?
  • 今后请始终使minimal reproducible example 可编译,而不是仅仅向我们扔代码片段。
  • 我需要1/2 出现在箭头旁边。我尝试了edge 命令,我得到了同样的错误
  • 尝试使用\draw[->] (a) -- (z1) node[midway,above] {$1/2$}; 如果仍然报错,您必须提供可编译的minimal reproducible example 而不是这些代码片段。
  • 对不起,我在我的问题中添加了完整的代码。 node[midway,above] 有效,但我认为错误是由于使用 [->] 而引起的,因为它仍然无法正常工作。

标签: latex tikz sharelatex


【解决方案1】:

有几个问题:

  • \draw 命令不像\node 命令那样采用标签。如果您想向箭头添加文本,有多种可能性,例如\draw [->] (a) -- (z1) node[midway,above] {$1/2$};

  • 那么你也在加载西班牙文 babel 包。这引入了一些速记来编写干扰 tikz 的西班牙语字符。关闭这些速记或加载 tikz babel

  • 请不要多次加载包

  • 查看nicefrac 包以获得更好的分数


\documentclass[spanish]{article}
\usepackage[utf8]{inputenc}

\usepackage[spanish]{babel}
%\usepackage[utf8]{inputenc}
\usepackage{fancyhdr} % Required for custom headers
\usepackage{lastpage} % Required to determine the last page for the footer
\usepackage{extramarks} % Required for headers and footers
\usepackage[usenames,dvipsnames]{color} % Required for custom colors
\usepackage{graphicx} % Required to insert images
\usepackage{listings} % Required for insertion of code
\usepackage{courier} % Required for the courier font
\usepackage{lipsum} % Used for inserting dummy 'Lorem ipsum' text into the template
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{natbib}
%\usepackage{graphicx}
%\usepackage{amsmath}
%\usepackage{amssymb}
\usepackage{tikz}

\usetikzlibrary{babel}

\begin{document}

\begin{tikzpicture}

\node (a) at (0,0) {$a$};    
\node (z1) at (3,2) {$z_1$};    
\node (z2) at (3,0) {$z_2$};    
\node (z3) at (3,-2) {$z_3$};

\draw [->] (a) -- (z1)  node[midway,above] {$1/2$};    
\draw [->] (a) -- (z2)  node[midway,above] {$1/4$};    
\draw [->] (a) -- (z3)  node[midway,above] {$1/4$};

\end{tikzpicture}

\end{document}

【讨论】:

    【解决方案2】:

    在节点间尝试edge命令

    \begin{tikzpicture}
    
    \node (a) at (0,0) {$a$};
    \node (z1) at (3,2) {$z_1$};
    \node (z2) at (3,0) {$z_2$};
    \node (z3) at (3,-2) {$z_3$};
    
    \draw [->] (a) edge (z1) (a) edge (z2) (a) edge (z3);
    
    \end{tikzpicture}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-06
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 2012-02-14
      • 2015-04-12
      • 2016-06-21
      相关资源
      最近更新 更多