How can I fix this error when using Tikz for Overleaf?

8.4k Views Asked by At

I'm trying to do a diagram with arrows. I have been able to produce the diagram but with no arrows using this code:

\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}

But I need the segments to be arrows and add tags to the segments but when I try this code:

\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}

I get an error in the first \draw line that says:

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


Runaway argument? 

Missing \endcsname inserted.

And then in the \end{tikzpicture} line I get:

Extra }, or forgotten \endgroup.

Missing } inserted. 

The complete code I'm using is this:

\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}
2

There are 2 best solutions below

0
On BEST ANSWER

There are a couple of problems:

  • the \draw command does not take a label like the \node command. If you want to add text to the arrow there are several possibilities, for example \draw [->] (a) -- (z1) node[midway,above] {$1/2$};

  • then you are also loading the spanish babel package. This introduces a couple of shorthands to write spanish characters which interfere with tikz. Either switch these shorthands off or load the tikz babel library

  • please don't load packages more than once

  • have a look at the nicefrac package to get nicer fractions


\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}

enter image description here

0
On

Try edge command between the nodes

\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}