6

I am trying to fill in the area under the curve of sin(x)/x. Here is my code:

\PassOptionsToPackage{usenames,dvipsnames,table,x11names}{xcolor}
\documentclass[a4paper, 12pt]{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{pgfplots}
\usepackage{tkz-euclide}
\pgfplotsset{compat=1.15}
\usetikzlibrary{arrows,shadows,positioning, calc, decorations.markings, hobby, quotes,angles,decorations.pathreplacing,intersections}
\usepgfplotslibrary{polar,colormaps,fillbetween}
\usepgflibrary{shapes.geometric}
\usepackage{xcolor}

\begin{document} 

\begin{figure}[ht]

\centering

\begin{tikzpicture}[scale=1.5,line width=1pt]
\begin{axis}[
color= black,
xmin=-1, 
xmax=7.9, 
ymin=-1, 
ymax=2, 
axis equal image, 
axis lines=middle, 
font=\scriptsize,
xtick distance=1,
ytick distance=1,
%xticklabels={}, 
%yticklabels={},
inner axis line style={stealth-stealth},
xlabel = {$x$},
ylabel = {$f(x)$},
ticks=none,
]

\addplot[black, opacity = 0, domain=0:10, name path = 1]{0};

\addplot[Tan, smooth, domain=0.01:7.5, samples = 300, name path=2, thick] {sin(deg(x))/x}; 

%\addplot[Tan, fill opacity=0.25] fill between [of=1 and 2,soft clip={domain=0:3.1415}];

\end{axis}
\end{tikzpicture} 

\caption{Integrating $\sin(x)/x$ from $0 \le x < \infty$.}
\label{1}

\end{figure}

\end{document}

I have the code in a larger document, so here is the output:

enter image description here

When I un-comment out the line to fill in the area, this happens:

enter image description here

There is a noticeable vertical gap and the image is pushed to the left. Any idea why this is happening?

8

The culprit is scale=1.5, which you should pass to the axis, and not to the tikzpicture. I guess you confuse pgfplots otherwise, see this answer. If you wish to have a core-level explanation, I am afraid I cannot provide it, but given this answer one of my first guesses was to change the position of scale, and it seems to work. Notice also that the pgfplots library fillbetween loads intersections, but a slightly advanced version, so you should not load intersections (again). I also removed tkz-euclide and unused libraries (in order to keep the code tidy), but you may resurrect them and the code still works as it should.

\PassOptionsToPackage{usenames,dvipsnames,table,x11names}{xcolor}
\documentclass[a4paper, 12pt]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{pgfplots}
%\usepackage{tkz-euclide}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{%polar,colormaps,
fillbetween}
% \usetikzlibrary{arrows,shadows,positioning, calc, decorations.markings, hobby, quotes,angles,decorations.pathreplacing}
% \usepgflibrary{shapes.geometric}
%\usepackage{xcolor}


\begin{document} 

\begin{figure}[ht]

\centering

\begin{tikzpicture}[line width=1pt]
\begin{axis}[scale=1.5,
color= black,
xmin=-1, 
xmax=7.9, 
ymin=-1, 
ymax=2, 
axis equal image, 
axis lines=middle, 
font=\scriptsize,
xtick distance=1,
ytick distance=1,
%xticklabels={}, 
%yticklabels={},
inner axis line style={stealth-stealth},
xlabel = {$x$},
ylabel = {$f(x)$},
ticks=none,
]
\addplot[black, opacity = 0, domain=0:10, name path = 1]{0};

\addplot[Tan, smooth, domain=0.01:7.5, samples = 300, name path=2, thick] {sin(deg(x))/x}; 

\addplot[Tan, fill opacity=0.25] fill between [of=1 and 2,soft clip={domain=0:3.1415}];

\end{axis}
\end{tikzpicture} 
\caption{Integrating $\sin(x)/x$ from $0 \le x < \infty$.}
\label{1}
\end{figure}
\end{document}

enter image description here

  • 2
    Perfect, thanks. I'm always curious about small issues like this because the solution is not at all obvious. – Aiden Kenny Mar 5 at 16:30

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.