6

I want to get consistent arrow tips for

  1. Arrows in TikZ drawings
  2. pgfplots axes
  3. Arrows used to annotate features in plots produced with pgfplots

My solution works only for 1 and 3, as demonstrated by this MWE:

\documentclass{standalone}

\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\pgfplotsset{compat=newest}

% does work for tikz arrows
% does work for pgfplots annotation arrows
% does NOT work for pgfplots axes
\tikzset{myarrow/.style={>={Computer Modern Rightarrow[scale length=1.5]}}}

\begin{document}

\begin{tikzpicture}
  \draw[->,myarrow] (0,0) -- (0,3);
  \draw[<-,myarrow] (1,0) -- (1,3);
\end{tikzpicture}

\begin{tikzpicture}
  \begin{axis}[width=4.0cm,height=4.0cm,xmin=0,xmax=1,ymin=0,ymax=1,
    xticklabels = {},yticklabels={},axis lines = middle,
    axis line style={myarrow}]
    \addplot {x^2};

    \draw[color=red   ,->,myarrow] (axis cs:0.0,0.4) -- (1.0,0.4);
    \draw[color=orange,<-,myarrow] (axis cs:0.0,0.6) -- (1.0,0.6);
  \end{axis}
\end{tikzpicture}

\end{document}

which results in

enter image description here

Note that the tips of the axes do not correspond to those of the other arrows. How can I define my arrow tip so it works for all three situations?

Notes:

  1. In a previous question, I asked about 2 only, and the solution leads to arrows that do not work for 1 and 3 (which work only for -> but not for <- in the sense that they always appear as ->).
  2. This is a significant reformulation of my question because it was unclear (sorry about that).
3

You just need a line style for axis line style e.g. an arrow with a myarrow tip. Like this:

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\pgfplotsset{compat=newest}
\tikzset{
  myarrow/.style={>={Computer Modern Rightarrow[scale length=1.5]}},
}
\begin{document}
\begin{tikzpicture}
  \draw[->, myarrow] (0,0) -- (0,3);
  \draw[<-, myarrow] (1,0) -- (1,3);
\end{tikzpicture}
%
\begin{tikzpicture}
  \begin{axis}[
    width=4cm, height=4.0cm,
    xmin=0, xmax=1,
    ymin=0, ymax=1,
    axis lines = middle,
    axis line style={->, myarrow}]
    \draw[red,->,myarrow] (0,0.4) -- (1,0.4);
    \draw[orange,<-,myarrow] (0,0.6) -- (1,0.6);
  \end{axis}
\end{tikzpicture}
%
\end{document}

Graph with myarrow arrows and axis

Do not use axis cs:. Unless you have an old version of PGFPlots, it is default.

3

Your style myarrow tells tikz to draw the arrow from left to right. In both of the following specifications,

<-,myarrow
->,myarrow

myarrow comes last and overwrites -> or <-.

What you probably want is to change only the arrow tip. For this, use

>={Computer Modern Rightarrow[scale length=1.5]}

either in the definition of myarrow

\tikzset{myarrow/.style={>={Computer Modern Rightarrow[scale length=1.5]}}}

or globally for the whole tikzpicture.

\begin{tikzpicture}[>={Computer Modern Rightarrow[scale length=1.5]}]

enter image description here

\documentclass{standalone}

\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\pgfplotsset{compat=newest}

\tikzset{myarrow/.style={>={Computer Modern Rightarrow[scale length=1.5]}}}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
      xmin=-2.0,xmax=2,
      ymin=0.0,ymax=4,
      enlargelimits=false,
    ]
    \addplot {x^2};
    \draw[color=green ,<-        ] (axis cs:0.0,1.0) -- (1.0,1.0);
    \draw[color=red   ,<-,myarrow] (axis cs:0.0,2.0) -- (1.0,2.0);
    \draw[color=orange,->,myarrow] (axis cs:0.0,3.0) -- (1.0,3.0);
  \end{axis}
\end{tikzpicture}
\end{document}
3
  • This is what I did beforehand, and why I asked my first question (tex.stackexchange.com/questions/585176/…). But then the definition does not work for the arrow tips in pgfplots. I realise now that my question did not make this sufficiently clear, so I will try again: I would like to have a single definition of a custom arrow tip that I can use in both TikZ and pgfplots, and in the latter in both annotations as well as axis tips. – user1362373 Feb 27 at 21:23
  • @user1362373 Please extend your example to contain arrows in all situations that are relevant. The current example is typeset correctly ... – gernot Feb 27 at 21:26
  • You are right of course, I reformulated my question. Hopefully it is clear. – user1362373 Feb 27 at 21:44
1

I have not been able to find a single definition of the arrow tip that works for all three situations, but here's a work-around in which I simply define a second style:

\documentclass{standalone}

\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\pgfplotsset{compat=newest}

\tikzset{
  myarrow/.style={>={Computer Modern Rightarrow[scale length=1.5]}},
  myaxisarrow/.style={-{Computer Modern Rightarrow[scale length=1.5]}}
}

\begin{document}

\begin{tikzpicture}
  \draw[->,myarrow] (0,0) -- (0,3);
  \draw[<-,myarrow] (1,0) -- (1,3);
\end{tikzpicture}

\begin{tikzpicture}
  \begin{axis}[width=4.0cm,height=4.0cm,xmin=0,xmax=1,ymin=0,ymax=1,
    xticklabels = {},yticklabels={},axis lines = middle,
    axis line style={myaxisarrow}]
    \addplot {x^2};

    \draw[color=red   ,->,myarrow] (axis cs:0.0,0.4) -- (1.0,0.4);
    \draw[color=orange,<-,myarrow] (axis cs:0.0,0.6) -- (1.0,0.6);
  \end{axis}
\end{tikzpicture}

\end{document}

I would have preferred a single definition, but I can live with this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.