2

I want draw a figure like fig 1, but my Latex give fig 2. I think the problem is in:

\shade[top ‎color=blue!2‎0‎‎] (‎-‎‎‎1,‎-1.2‎‎‎‎‎‎‎‎) rectangle (‎3‎‎‎,‎3‎);‎‎

‎‎enter image description here

Here is the Latex code:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\draw[decoration={aspect=0.3, segment length=1.5mm, amplitude=2.4mm,coil},decorate] (1,5) -- (1,3.3);
\fill [pattern = north east lines] (-0.5,5) rectangle (2.5,5.2);
\draw[thick] (-0.5,5) -- (2.5,5);
%\filldraw[fill=blue!20, draw=blue!60] (-1,-1.2) rectangle (3,3);
\draw[line width=0.5pt] (1,3.3) -- (1,2.5);
\draw[line width=1pt] (-1,3) -- (3,3);

\draw[->] (1,0.2) -- (1,-0.8);
\node[draw=none,right=.1cm] at (1.3,4.25) (a) {$K$};
\node[draw=none,right=.1cm] at (0,-0.5) (a) {$f(t)$};
\draw[line width=6pt] (1,2.5) -- (1,-0.2);
\shade[top color=blue!20] (-1,-1.2) rectangle (3,3);
\end{tikzpicture}
\end{document}

The output is not correct!

10
  • You only uploaded one image. Is this image what the result should look like?
    – hbaderts
    Oct 9, 2016 at 17:51
  • sorry I can not upload incorrect figure.
    – Nima
    Oct 9, 2016 at 17:55
  • However, you can run this code and obtained it
    – Nima
    Oct 9, 2016 at 17:56
  • Ok, no problem, so the result should look like the image you have shown?
    – hbaderts
    Oct 9, 2016 at 17:56
  • yes, I have a problem with "\documentclass{standalone}".
    – Nima
    Oct 9, 2016 at 17:59

1 Answer 1

1

When drawing a TikZ picture, you should always start with things in the background, and then add objects which are in front of that. Take the following example:

\draw[red,fill] (0,0) circle (0.1);
\draw[blue,fill] (-1,-1) rectangle (1,1);

The result of this will be only the rectangle, as the circle is behind the rectangle.

The same happened to your figure: By placing the \shade command at the bottom of your code, i.e. as the last command, this shaded rectangle is shown in the foreground, while you actually want it to be in the background. As a solution, simply move the command to the start of your tikzpicture:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}

% First draw the background
\shade[top color=blue!20] (-1,-1.2) rectangle (3,3);

\draw[decoration={aspect=0.3, segment length=1.5mm, amplitude=2.4mm,coil},decorate] (1,5) -- (1,3.3);
\fill [pattern = north east lines] (-0.5,5) rectangle (2.5,5.2);
\draw[thick] (-0.5,5) -- (2.5,5);
%\filldraw[fill=blue!20, draw=blue!60] (-1,-1.2) rectangle (3,3);
\draw[line width=0.5pt] (1,3.3) -- (1,2.5);
\draw[line width=1pt] (-1,3) -- (3,3);

\draw[->] (1,0.2) -- (1,-0.8);
\node[draw=none,right=.1cm] at (1.3,4.25) (a) {$K$};
\node[draw=none,right=.1cm] at (0,-0.5) (a) {$f(t)$};
\draw[line width=6pt] (1,2.5) -- (1,-0.2);
\end{tikzpicture}
\end{document}

and you'll get exactly the desired result:

result

6
  • sorry, but in my result the background is disappear!
    – Nima
    Oct 9, 2016 at 18:07
  • If you copy&paste exactly the code in my answer to a new document and compile it, do you get the same result as my image?
    – hbaderts
    Oct 9, 2016 at 18:08
  • I done it, but ...., I think it is a counterexample. maybe my computer does not work correct!
    – Nima
    Oct 9, 2016 at 18:13
  • Do you have an up-to-date latex installation? You can open you xy.log file, and the first line should say sth like "This is pdfTeX, Version ..., (TeX Live 2016)" - I am interested in the last part: can you post the "TeX Live 20xx" (maybe it is also "MikTeX xyz") here?
    – hbaderts
    Oct 9, 2016 at 18:17
  • many thanks. If it is possible to you, please send to me .EPS of figure. I think my Latex is old and does not work well. ([email protected])
    – Nima
    Oct 9, 2016 at 18:23

You must log in to answer this question.

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