Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Following the first tutorial in the TikZ manual, I tend to take steps to improve the legibility of tick labels when placed over gridlines. I prefer to use a fading rather than a plain white rectangle. However, if two nodes with faded background get placed right next to each other, one will "fade" over the text of the other. Putting them all in a transparency group just seems to kill the fading effect altogether.

How do I make the fadings in two overlapping nodes not act on each other's text? (Preventing their fadings from stacking would be nice also, but is less important.)

Unfortunately, I was unsuccessful in uploading images that demonstrate the transparency, but here's the code for some minimal working examples:

What it's supposed to look like, more or less:

\documentclass[tikz]{standalone}
%
%With the following code (from the TikZ manual) installed, 
%the option "path fading=fade out"
%will cause a filled shape or node to be opaque in the center and 
%gradually fade to transparency at the edges.
\usetikzlibrary{fadings}
\tikzfading[name=fade out,
            inner color=transparent!0,
            outer color=transparent!100]
%
\begin{document}
\begin{tikzpicture}[scale=1.0]
    \draw[help lines] (-4.9,-4.9) grid (4.9,4.9);
    \draw[thin,->] (-5,0) -- (5,0) node[right]{$x$};
    \draw[thin,->] (0,-5) -- (0,5) node[above]{$y$};
    \begin{scope}[ticklabel/.style={fill=white,path fading=fade out,text opacity=1.0}]
    \foreach \x in {-4,-3,-2,-1,1,2,3,4}
    {
        \node[below,ticklabel] at (\x,0) {$\x$};
    }
    \foreach \y in {-4,-3,-2,-1,1,2,3,4}
    {
        \node[left,ticklabel] at (0,\y) {$\;\y$};
    }
    \end{scope}
\end{tikzpicture}
\end{document}

What it looks like when nodes are too close together, with background rectangles to help illustrate the effect:

\documentclass{standalone}
\usepackage{tikz}
%
%With the following code (from the TikZ manual) installed, 
%the option "path fading=fade out"
%will cause a filled shape or node to be opaque in the center and 
%gradually fade to transparency at the edges.
\usetikzlibrary{fadings}
\tikzfading[name=fade out,
            inner color=transparent!0,
            outer color=transparent!100]
%
\begin{document}
\begin{tikzpicture}[scale=0.4]
    \fill[black!20] (-5,-5) rectangle (5,5);
    \draw[help lines] (-4.9,-4.9) grid (4.9,4.9);
    \draw[thin,->] (-5,0) -- (5,0) node[right]{$x$};
    \draw[thin,->] (0,-5) -- (0,5) node[above]{$y$};
    \begin{scope}[ticklabel/.style={fill=white,path fading=fade out,text opacity=1.0}]
    \foreach \x in {-4,-3,-2,-1,1,2,3,4}
    {
        \node[below,ticklabel] at (\x,0) {$\x$};
    }
    \foreach \y in {-4,-3,-2,-1,1,2,3,4}
    {
        \node[left,ticklabel] at (0,\y) {$\y$};
    }
    \end{scope}
\end{tikzpicture}
\begin{tikzpicture}[scale=0.4]
    \fill[black!20] (-5,-5) rectangle (5,5);
    \draw[help lines] (-4.9,-4.9) grid (4.9,4.9);
    \draw[thin,->] (-5,0) -- (5,0) node[right]{$x$};
    \draw[thin,->] (0,-5) -- (0,5) node[above]{$y$};
    \begin{scope}[ticklabel/.style={fill=white,path fading=fade out,text opacity=1.0},transparency group]
    \foreach \x in {-4,-3,-2,-1,1,2,3,4}
    {
        \node[below,ticklabel] at (\x,0) {$\x$};
    }
    \foreach \y in {-4,-3,-2,-1,1,2,3,4}
    {
        \node[left,ticklabel] at (0,\y) {$\y$};
    }
    \end{scope}
\end{tikzpicture}
\end{document}

The example on the right shows the effect of putting the nodes inside a transparency group.

share|improve this question

1 Answer 1

Not very sophisticated … (but then: Why so tight ticks? Why not pgfplots?)

Code (the scope)

\begin{scope}[ticklabel/.style={fill=white,path fading=fade out,text opacity=1.0}]
    \foreach \x in {-4,-3,-2,-1,1,2,3,4}{
        \node[below,ticklabel] at (\x,0) {$\phantom{\x}$};
        \node[left,ticklabel] at (0,\x) {$\phantom{\x}$};
    }
    \foreach \x in {-4,-3,-2,-1,1,2,3,4}{
        \node[below] at (\x,0) {$\x$};
        \node[left] at (0,\x) {$\x$};
    }
\end{scope}

Output

enter image description here

share|improve this answer
    
How did you get a png file that showed the transparency? –  Charles Staats Nov 8 '12 at 23:06
    
In answer to your questions: I find the pgfplots manual unfriendly to someone whose primary interest is just in drawing axes, so I decided to create my own macro. (Also, so far as I know, pgfplots cannot create the sort of fading I am using.) The "tight ticks" are deliberately exaggerated in the examples I gave here, but a less egregious version of this problem showed up with reasonably spaced ticks when I used ellipses rather than rectangles for the nodes' background shapes. –  Charles Staats Nov 8 '12 at 23:17
    
@CharlesStaats You may be interested in tkz-fct or some of the other packages by Altermundus/Alain Matthes. Though, I don't think that such detailed settings are possible. And about png: a simple screenshot does show them, too … –  Qrrbrbirlbel Nov 8 '12 at 23:28
    
@CharlesStaats Ticklabels in pgfplots are also nodes with accesible style specs (since completely built on TikZ). If you include your real example, I'm certain that it would be solvable much easier than manually drawing your own axes. Otherwise you can send the fadings to the background layer so they are not interfering the text. –  percusse Nov 9 '12 at 1:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.