0

I want to replicate this figure using tikz. enter image description here

Right now I am stuck with the lower part of the figure, the ground. I tried using foreach \x command, but for some reason I do not understand, it is no working. First, I tried using the xshift option in the draw command.

\documentclass{standalone}
\standaloneconfig{border=5mm 5mm 5mm 5mm}
\usepackage{units}
\usepackage{tikz,pgf,import,pgfplots} 
\usetikzlibrary{calc, pgfplots.units,}
\pgfplotsset{compat=1.3} 
\begin{document}
     \centering
\begin{tikzpicture}
\begin{axis}[xmin=-5, xmax=5, ymin=-5, ymax=5,
            xtick=\empty, ytick=\empty, axis lines=center]
    %Sine wave
    \addplot[blue, domain=-4:4]  {sin(deg (x))};
    % Ground
    \draw(axis cs: -4,-4.5)--(axis cs: 4,-4.5);
    \foreach \x in {0.2, 0.4, ..., 8} {%
        \draw[xshift={axis cs: \x} ] (axis cs: -4.0 ,-5) -- (axis cs: -3.8,-4.5);
        % \draw (axis cs: { -4.0 + \x },-5) -- (axis cs: { -3.8 + \x },-4.5);
        }%
    % \draw[hatch](axis cs: -4,-4.5)--(axis cs: 4,-4.5);
\end{axis}
\end{tikzpicture}

\end{document}

But it is not working, I just get the first inclined line:

enter image description here

I also tried doing the shift "manually" on the coordinates:

        \foreach \x in {0.2cm, 0.4cm, ..., 8cm} {% I tried adding the units cm amd whitout
             \draw (axis cs: { -4.0 + \x },-5) -- (axis cs: { -3.8 + \x },-4.5);
        }%

But in this case, the file does not even compile.

Looking for solutions, I found in this example an option using path decoration.

So I added a new style:

    \begin{tikzpicture}[hatch/.style={postaction={line cap=round,draw,decorate,decoration={border,angle=-120, amplitude=0.4cm,segment length=2mm}}}]%

\begin{axis}[xmin=-5, xmax=5, ymin=-5, ymax=5, xtick=\empty, ytick=\empty, axis lines=center] %Sine wave \addplot[blue, domain=-4:4] {sin(deg (x))}; % Ground \draw[hatch](axis cs: -4,-4.5)--(axis cs: 4,-4.5); \end{axis} \end{tikzpicture}

In this case I get the hatching, but it does not fill the entire path.

enter image description here

Any idea how to solve this issue (which whatever solution).

Thanks in advance!

Cheers!

Bernard
  • 271,350

1 Answers1

2

See this question and the related answer which explains why you cannot use \foreach inside an axis environment. Or actually, you can, but you need to make a macro out of it to make sure that expansion and calculations work correctly.

Applied to your problem, you could do:

\documentclass{standalone}
\standaloneconfig{border=5mm 5mm 5mm 5mm}
\usepackage{tikz, pgfplots} 
\usetikzlibrary{calc}
\pgfplotsset{compat=1.3} 
\begin{document}
     \centering
\begin{tikzpicture}
\begin{axis}[xmin=-5, xmax=5, ymin=-5, ymax=5,
            xtick=\empty, ytick=\empty, axis lines=center]
    % Sine wave
    \addplot[blue, domain=-4:4]  {sin(deg (x))};
    % Ground
    \draw(axis cs: -4,-4.5)--(axis cs: 4,-4.5);
    \foreach \x in {0.2, 0.4, ..., 8} {%
        \edef\temp{\noexpand\draw (axis cs: { -4.0 + \x }, -5) -- (axis cs: { -3.8 + \x }, -4.5);}
        \temp
        }%
\end{axis}
\end{tikzpicture}
\end{document}

Or also:

\documentclass{standalone}
\standaloneconfig{border=5mm 5mm 5mm 5mm}
\usepackage{tikz, pgfplots} 
\usetikzlibrary{calc}
\pgfplotsset{compat=1.3} 
\begin{document}
     \centering
\begin{tikzpicture}
\begin{axis}[xmin=-5, xmax=5, ymin=-5, ymax=5,
            xtick=\empty, ytick=\empty, axis lines=center]
    % Sine wave
    \addplot[blue, domain=-4:4]  {sin(deg (x))};
    % Ground
    \draw(axis cs: -4,-4.5)--(axis cs: 4,-4.5);
    \pgfplotsinvokeforeach{0.2, 0.4, ..., 8} {%
        \draw (axis cs: { -4.0 + #1 }, -5) -- (axis cs: { -3.8 + #1 }, -4.5);
    }%
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here