3

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...

Ricardo Cruz
  • 1,770
  • 1
  • 22
  • 34

2 Answers2

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

Kpym
  • 23,002
  • 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
3

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

Ricardo Cruz
  • 1,770
  • 1
  • 22
  • 34