4

In a TikZ picture, I want a white shaded rectangle to overlap perfectly a black rectangle. Here is a MWE of what I have done so far. (In the MWE, the shading is much more simple than the one I want to actually apply.)

\documentclass{article}

\usepackage[letterpaper,textwidth=8.5in,textheight=11in]{geometry}
\usepackage{lscape}

\usepackage{tikz}
\usetikzlibrary{fadings}

\begin{document}
\pagestyle{empty}
\begin{landscape}
\begin{figure}
\centering

\begin{tikzfadingfrompicture}[name=fondu, scale=1]

\shade[left color=transparent!100,
right color=transparent!0] (0,0) rectangle ++(0.15,-0.35);

\shade[left color=transparent!0,
right color=transparent!0] (0.15,0) rectangle ++(0.50,-0.35);

\shade[left color=transparent!0,
right color=transparent!100] (0.50,0) rectangle ++(1,-0.35);

\end{tikzfadingfrompicture}

\begin{tikzpicture}[scale=1]

\coordinate(A) at (1.5,4);
\draw [thick, fill=black] (A) rectangle ++(1,-0.35);
\fill [path fading=fondu, white] (A) rectangle ++(1,-0.35);

\end{tikzpicture}
\end{figure}
\end{landscape}
\end{document}

With this MWE, I obtain this:

enter image description here

It is as though the scale was not the same for the tikzfadingfrompicture than for the actual tikzpicture...

How could I obtain the desired result (a perfect overlap between the white shaded rectangle and the black rectangle underneath)?

This is what I want (but with the type of fading of the image above):

enter image description here

  • Don't understand when you say "perfectly". Can you show a picture of what you want? – juanuni May 24 '15 at 2:54
  • The picture is there. I know I could accomplish it with path fading=east, but this is only an example. In reality I will do a more intricate fading shape. – Melian May 24 '15 at 3:00
  • Well I don't know what you have in mind, so is a little bit that somebody help you ... – juanuni May 24 '15 at 3:10
  • I edited my post to include exactly the fading I want. – Melian May 24 '15 at 12:59
2

From showed your desired picture, I gues that you looking for something like this:

enter image description here

This picture I obtain with:

\documentclass{article}
    \usepackage[letterpaper,textwidth=8.5in,textheight=11in]{geometry}
    \usepackage{lscape}

    \usepackage{tikz}
    \usetikzlibrary{fadings}
    \tikzfading[name = fondu,
         left color = transparent!0,
         right color = transparent!100]

    \begin{document}
\pagestyle{empty}
\begin{landscape}
    \begin{figure}
    \centering

\begin{tikzpicture}
    \coordinate(A) at (1.5,4);
\draw [thick,fill=black] (A) rectangle ++(1,-0.35);
\fill [path fading=fondu, white] (A) rectangle ++(1,-0.35);
\end{tikzpicture}
    \end{figure}
\end{landscape}
    \end{document}

I don't understand, what you like to obtain with:

\begin{tikzfadingfrompicture}[name=fondu, scale=1]
\shade[left color=transparent!0,
right color=transparent!100] (0,0) rectangle ++(1,-0.35);
\end{tikzfadingfrompicture}

so replace width standard definition of fading in preamble.

Update: I don't know of context of your WME, so I just edit your MWE so that it work in simple cases. For more general solution wee if the following MWE is good starting point for your need:

\documentclass[tikz,border=1mm]{standalone}
    \usetikzlibrary{fadings,fit,positioning}
    \tikzfading[name = fondu,
         fading angle=45,
         fill=black,
         scope fading=south,
                ]

    \begin{document}
\begin{tikzpicture}[
mynode/.style n args = {3}{
    name=#1,
    fading angle=#2,
    fill=#3,
    scope fading=west, 
    node contents={}% <-- only if you will have always empty rectangle ...
                           },
myframe/.style 2 args = {
    draw=#1,% <-- color of node frame, if it is constant, write color name
            % and redefine node style accordingly
    ultra thick,
    fit=(#2),
    inner sep=0pt, outer sep=0pt,
    node contents={}},
                    ]
    \coordinate(A) at (1.5,4);
\node[mynode={bla}{30}{black},
      minimum width=33mm,%select desired size
      minimum height=11mm,%select desired size
      above right=of A];
\node[myframe={black}{bla}];
\end{tikzpicture}
    \end{document}

This time I use standalone package/document class to easier show obtained picture. Instead to draw rectangular I use rectangular shape as node (this is better solution so long your need only nodes defined in shapes library). The first node determine shadings, the second one frame around it.

Shading now can be oriented according to your needs. I hope that above MWE is enough self explanatory.

In case that the aspect ratio between width and height of rectangle is constant, you can determine in mynode/.style nominal size of rectangle and later with scale re-size it to desired size.

Obtained figure: enter image description here

| improve this answer | |
  • 1
    This works for my previous example with the simple fading vanishing from left to right, but with the exact fading I want (illustrated above in the edited question), I have more than a left and a right color (in fact the fading picture is the assembly of three rectangles, as can be seen in the code). – Melian May 24 '15 at 13:02

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.