6

I would like to add an arrowhead in the middle of a plot but mark is not working. I also tried \arrow[red]{stealth} instead of > but it gives me an error. Here is my code

\documentclass[a4paper,12pt]{book}

\usepackage{fontspec}
\usepackage{amsmath,empheq}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{patterns}
\usetikzlibrary{calc}

\begin{document}

\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\begin{axis}[
axis on top,
width=0.8\textwidth,
axis equal image,
xmin=0,
xmax=4,
ymin=0,
ymax=4,
]
\path[name path=axis] (1,0)--(3,0);
\addplot[name path=f,smooth,mark=at position 2 with >,domain=1:3,samples=200,blue,thick]{12/(x+3)};
\addplot[yellow] fill between[of=f and axis];
\draw[dashed] (1,3)--(1,0);
\draw[dashed] (3,2)--(3,0);
\fill (1,3) circle (1.5pt);
\fill (3,2) circle (1.5pt);
\end{axis}
\end{tikzpicture}
\caption{}
\label{}
\end{figure}

\end{document}

And here is the image I get that is missing the arrowhead.

enter image description here

This is the code updated following the suggestion of user LaTexDraw-com

\documentclass[a4paper,12pt]{book}

\usepackage{fontspec}
\usepackage{amsmath,empheq}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{patterns}
\usetikzlibrary{calc}

\tikzstyle witharrow=[postaction={decorate,decoration={markings,mark=at position 0.5 with {\arrow[#1]{stealth}}}}] 

\begin{document}

\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\begin{axis}[
axis on top,
width=0.8\textwidth,
axis equal image,
xmin=0,
xmax=4,
ymin=0,
ymax=4,
]
\path[name path=axis] (1,0)--(3,0);
\addplot[name path=f,domain=1:3,samples=200,blue,thick,witharrow=red]{12/(x+3)};
\addplot[yellow] fill between[of=f and axis];
\draw[dashed] (1,3)--(1,0);
\draw[dashed] (3,2)--(3,0);
\fill (1,3) circle (1.5pt);
\fill (3,2) circle (1.5pt);
\end{axis}
\end{tikzpicture}
\caption{}
\label{}
\end{figure}

\end{document}

but now I have another problem, the filling is wrong as you can see from the image below

enter image description here

3 Answers 3

4

You do neither need decorations nor intersections. (BTW, your dimension too large error may go away with /pgf/fpu/install only={reciprocal}, see pgfmanual 3.1.8 section 50.2 on p. 640.)

\documentclass[a4paper,12pt]{book}

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


\begin{document}

\begin{figure}[htbp]
\centering
\begin{tikzpicture}[pics/arrow/.style={code={
    \draw[blue,-{Stealth[color=red,length=#1]}] (-#1/2,0) -- (#1/2,0);}},
    pics/arrow/.default=3mm,
    bullet/.style={circle,fill,inner sep=0pt,minimum size=3pt}]
\begin{axis}[
axis on top,
width=0.8\textwidth,
axis equal image,
xmin=0,
xmax=4,
ymin=0,
ymax=4,
]
\path[name path=axis] (1,0)--(3,0);
\addplot[name path=f,domain=1:3,smooth,blue,thick]{12/(x+3)}
 node[pos=0,bullet](b1){} pic[pos=0.5,sloped]{arrow} node[pos=1,bullet](b2){};
\addplot[yellow] fill between[of=f and axis];
\draw[dashed] foreach \x in {1,2}{(b\x) -- (b\x|-0,0)};
\end{axis}
\end{tikzpicture}
\caption{No decorations.}
\label{fig:No}
\end{figure}

\end{document}

enter image description here

2
  • Thank you for your help. The bullet and arrow are defined locally for the plot, right? Is there a way to define them globally in the preamble, for example with tikzstyle, in order to use them in different plots without repeating the code?
    – Emmet
    Jan 1, 2021 at 9:51
  • 1
    @Emmet Sure, just add \tikzset{pics/arrow/.style={code={ \draw[blue,-{Stealth[color=red,length=#1]}] (-#1/2,0) -- (#1/2,0);}}, pics/arrow/.default=3mm, bullet/.style={circle,fill,inner sep=0pt,minimum size=3pt}} to the preamble of the document.
    – user232027
    Jan 1, 2021 at 15:38
4

Here is my attempt:

\documentclass[a4paper,12pt]{book}

\usepackage{fontspec}
\usepackage{amsmath,empheq}
% \usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{patterns}
\usetikzlibrary{calc}

% Arrow style
\tikzset{decorated arrows/.style={
    postaction={
        decorate,
        decoration={
            markings,
            mark=at position 0.5 with {\arrow{stealth}}
            }
        },
    }
}

\begin{document}

\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\begin{axis}[
axis on top,
width=0.8\textwidth,
axis equal image,
xmin=0,
xmax=4,
ymin=0,
ymax=4,
]
\path[name path=axis] (1,0)--(3,0);
\addplot[name path=f,smooth,domain=1:3,blue,thick]{12/(x+3)};
\addplot[smooth,domain=1:3,blue,thick,decorated arrows]{12/(x+3)};
\addplot[yellow] fill between[of=f and axis];
\draw[dashed] (1,3)--(1,0);
\draw[dashed] (3,2)--(3,0);
\fill (1,3) circle (1.5pt);
\fill (3,2) circle (1.5pt);
\end{axis}
\end{tikzpicture}
\caption{}
\label{}
\end{figure}

\end{document}

which yields:

enter image description here

I think there is an issue between fill between and markings. Check this post "How to draw an arrowhead in the middle of a path" for more details

2
  • Thank you for your help. Unfortunately it gives me the error "! dimension too large" when i compile, and then with this way the curve is traced two times, one over the other (even if it is barely visible). Other than that, this solution is good. Any idea on how I can get rid of that error?
    – Emmet
    Dec 31, 2020 at 16:07
  • I was able to get rid of the "dimension too large error" by removing the smooth option. But now the filling is wrong.
    – Emmet
    Dec 31, 2020 at 16:14
4

With use of intersections library for determine coordinates of start and end of function, softh clip on reversed domain:

\documentclass[12pt]{article}
\usepackage{fontspec}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.17}
\usetikzlibrary{arrows.meta,
                decorations.markings,
                intersections}
\tikzset{
    ->-/.style = {decoration={markings,         % Arrow style
                  mark=at position 0.5 with {\arrow{Stealth}}
                              },                      
                  postaction={decorate},
                  ultra thick, color=blue},
    dot/.style = {circle, fill, inner sep=2pt}  % black circle
        }
\newcommand\Ymax{\pgfkeysvalueof{/pgfplots/ymax}}

\begin{document}
   \begin{figure}[htbp]
    \centering
    \begin{tikzpicture}
\begin{axis}[
axis on top,
width=0.8\textwidth,
axis equal image,
xmin=0, xmax=4,
ymin=0, ymax=4,
tick label style = {font=\footnotesize},
        domain=1:3, 
samples=61
            ]
\path[name path=sgmnt] (1,\Ymax) |- (3,0) -- (3,\Ymax);
\addplot[name path=f,postaction={decorate}] {12/(x+3)};
\addplot[yellow] fill between  [of=sgmnt and f, 
                                soft clip={domain=3:1}]; % <--- inverted domain!
\addplot[->-] {12/(x+3)};   % <--- function with arrow head on the middle
%
\draw   [name intersections={of=sgmnt and f, by={a,b}}]
        [dashed] (1,0) -- (a) node[dot] {}
                 (3,0) -- (b) node[dot] {};
\end{axis}
    \end{tikzpicture}
\caption{My beautiful Diagram}
\label{fig: Field under function}
    \end{figure}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .