2

I want to draw a line with a shade, not a big rectangle.

As per the documentation,

Shading is caused by the shade option (there are \shade and \shadedraw abbreviations).

Shouldn't therefore something like this work?

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw [shade, left color=red, right color=blue] (0,0) -- (1,0);
\end{tikzpicture}
\end{document}

But I cannot get it to be shaded. It always shows up like this:line


In actuality, what I want to draw is a rectangle where the end of the borders are shaded this way:

rectangle with shaded borders

There is probably a simple way to do this, but I only see forms of doing filled shade, not border shades or single lines...

  • 1
    Related/possible duplicate: Tikz: Shading a path without any filling – Claudio Fiandrino Jun 23 '18 at 10:18
  • 3
    Shading is only for areas, not lines. Edit: Also from the documentation: "A shading is like a filling, ..." – Torbjørn T. Jun 23 '18 at 10:23
  • @ClaudioFiandrino I saw that solution, but it's overkill for my case. I don't want to use more than 100 lines when I just need a straight line. I will just draw lines as rectangles, as the answer below. – Ricardo Cruz Jun 23 '18 at 13:25
5

You can use path fading from fadings library.

\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{fadings}
\begin{document}
  \tikz
    \draw[blue,thick,path fading=east] (0,0) rectangle (4,1);
\end{document}

enter image description here

  • 1
    Thank you. I had tried that, but it also didn't work. I see now that solution doesn't work in the PDF viewer I use (Evince). I will go with the answer I posted in the comments (for interoperability's sake). – Ricardo Cruz Jun 23 '18 at 13:22
2

As @Torbjørn T. pointed out, shading is a filling.

I have decided to just use rectangles of the size of a line (\pgflinewidth).

i.e.

\documentclass[border=1mm]{standalone}
\usepackage{tikz}

\newcommand{\fadehline}[3]{
    \fill (#1,#3) rectangle ++(#2, \pgflinewidth);
    \shade[left color=black, right color=white] (#2,#3) rectangle ++(0.5, \pgflinewidth);
}

\begin{document}
\begin{tikzpicture}

\fadehline{0}{1}{0};
\fadehline{0}{1}{0.8};
\fill (0,0) rectangle ++(\pgflinewidth, 0.8);

\end{tikzpicture}
\end{document}

enter image description here

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.