4

I am trying to add shadow to node. For normal node, the shade will not impact the label. However, for nodes inside a matrix, the labels are shadowed too.

MWE below

\documentclass[border=5mm, convert]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes, positioning, matrix, shadows}

\begin{document}
    \begin{tikzpicture}[auto]
        \tikzset{myrect/.style={draw, rectangle, font=\ttfamily, fill=white, drop shadow}}
        \tikzset{mylabel/.style={color=blue, font=\tiny \ttfamily}}
        \node[myrect, label={[mylabel]above:normal label}] (ele) {Single Element};

        \matrix (layer) [matrix of nodes, nodes={myrect, anchor=center}, column sep = 1cm, below = of ele] {
            |[label={[mylabel]:matrix label 1}]|matrix element 1 &
            |[label={[mylabel]:matrix label 2}]|matrix element 2 &
            |[label={[mylabel]:matrix label 3}]|matrix element 3 \\
        };
    \end{tikzpicture}
\end{document}

The output is as the following:enter image description here

How this happen and is there way to avoid it?

  • 2
    The nodes= key for the matrix is like a shortcut for every node/.style= and since labels are also nodes, they also get the drop shadow. I suspect that some keys, like draw are reset within a label, but others like drop shadow are not. – Max Jan 11 at 9:54
  • 2
    You could use the no shadows key from solution B from this answer and add it to your mylabel style. That would delete all preactions added to the labels, including the drop shadow. – Max Jan 11 at 10:06
5

One way is to adjust the your style-set definitions (as done by @marmot):

 \documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{shadows, shapes, positioning, matrix}
\makeatletter % from https://tex.stackexchange.com/a/129322/121799
\tikzset{no shadows/.code=\let\tikz@preactions\pgfutil@empty}
\makeatother
%https://tex.stackexchange.com/questions/422782/labels-of-nodes-with-drop-shadow
\begin{document}
    \begin{tikzpicture}[
    every node/.style = {font= \ttfamily, draw, fill=white, drop shadow},
    every label/.append style = {
        font=\tiny \ttfamily,
        color = blue,
    }
    ]
    \node [label={[no shadows]foobar}]  (ele) {node with shadow};
    \matrix (layer) [no shadows, matrix of nodes, draw = none, column sep = 1cm, below = of ele] {
        |[label={[no shadows]:matrix label 1}]|matrix element 1 &
        |[label={[no shadows]:matrix label 2}]|matrix element 2 &
        |[label={[no shadows]:matrix label 3}]|matrix element 3 \\
    };
    \end{tikzpicture}
\end{document}

This should give you:

enter image description here

  • 1
    Thank you, both you and @Max point me to solution of disable preactions. I am still wondering if there can be options in matrix to avoid polluting the label – Eric Sun Jan 11 at 10:26
  • @EricSun Also, as far as not to pollute the matrix environment, I dont know. May be I can look it up. – Raaja Jan 11 at 10:30
  • 1
    Don't mark accepted because I am afraid people may stop entering this post when they find there has already been an answer. Anyway I have checked it now :) – Eric Sun Jan 11 at 10:31

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.