10

When using every x tick label/.append style={font=\scriptsize} in combination with \pgfplotsset outside the tikzpicture environment, the pgfplotssetcommand from the preamble will be ignored (respectivly overwritten) by every x tick label/.append style={font=\color{black}} within the axis environment of the tikzpicture (note: the command within the axis environment is not related to the font size as the pgfplotset command in the preable and should therefore not overwrite information about the fontsize).

Example Code:

 \documentclass[12pt,a4paper,tikz]{standalone}
 \usepackage[T1]{fontenc}
 \usepackage[utf8]{inputenc}
 \usepackage{lmodern}
 \usepackage{tikz,pgfplots,grffile,amsmath}
 \pgfplotsset{compat=newest}
 \pgfplotsset{mytikzstyle/.style={
     every axis/.append style={
         legend style={
             font=\tiny, % within the legend, fontsize changes work as there is no legend command in the axis environment that overwrites it...
             },
         },
     every x tick label/.append style={font=\scriptsize}, % won't work as it seems to be deleted/overwritten by .append style in the axis environment
     every y tick label/.append style={font=\scriptsize}, % will work, because there is no .append style command in the axis environment
     %every tick label/.append style={font=\scriptsize}, % works neither ...
     },}
 \pgfplotsset{mytikzstyle}
 \pgfplotsset{/pgfplots/width=8cm}
 \pgfplotsset{/pgfplots/height=6cm}

 \begin{document}
 \begin{tikzpicture}
 \begin{axis}[%
 separate axis lines,
 every x tick label/.append style={font=\color{black}},
 %every x tick label/.append style={font=\scriptsize}, % if I'd uncomment this line, the x tick labels would be properly displayed in \scriptsize
 %every y tick label/.append style={font=\color{black}}, % if I'd uncomment this line, the font change of the y-axis wouldn't work either
 legend style={at={(0.99,0.01)}, anchor=south east, legend cell align=left, align=left, draw=gray},
 ]
 \addplot [line width=0.8pt, color=black]
   table[row sep=crcr]{%
 1  1\\
 100    100\\
 };
 \addlegendentry{Hello $U_{1}\frac{xyz}{xyz}$}
 \end{axis}
 \end{tikzpicture}%
 \end{document}

example

As you can see: y tick label fontsize change works, as there is no command within the axis environment that will overwrite the pgfplots command (although I am only using append everywhere, anyway) x tick label fontsize change doesen't work as it seems to be overwritten by the x tick label command in the axis environment.

I really don't understand why this is like it is. Is it intended? My problem is actually that the axis code is generated by matlab2tikz in my case and I want to have an easy and convenient possibility to change styles from outside the tikzspicture environment with predefined pgplotsets/styles in LaTeX. But if the styles are overwritten by appended commands within the axis environment, it becomes useless...

Thanks in advance for any further explanation and how to avoid problems like this in future!

FINAL EDIT: The problem is a side effect of the font=\color{black} command, which overwrites other font properties, such as \tiny and therefore the pgfplotsset command that should affect the fontsize won't be appended (respectivly is resetted to \normalsize). Replacing the command by text=black will solve this problem, as text=black does not overwrite all other font settings. It was reported at github (matlab2tikz) and already fixed in the development branch of matlab2tikz, as the code with font=\color{black} was generated from it. The solution can also be applied by find and replacing font=\color{yourcolor} by text=yourcolor or in the matlab2tikz.m file as shown here by finding ['{font=\color{',col,'}}']); and replacing by ['{text=',col,'}']); in matlab2tikz.m, to prevent matlab2tikz from creating the side affected code again.

Unfortunately, I just realized that there are more such commands (exported by matlab2tikz), e.g. title style={font=\bfseries}, which will overwrite all color or size settings of the title that were defined with pgfplotsset before... Another example is ylabel style={font=\color{white!15!black}}, which will reset all y label settings to the default value and then applies only the color change.

Ultimately, this particular problem can be considered solved, as it is understood why the pgfplotsset commands are overwritten and not appended as expected and how this can be fixed, but with respect to matlab2tikz there is a lot more to do.

10
  • 1
    Apparently not all people share your opinion. All local settings override global one. Less obvious seems to be, that your font=\color{black} actually means: set normal font in black. To change only color, correct approach is text= <color>.
    – Zarko
    Sep 13, 2016 at 8:15
  • @Zarko I don't understand your example with text= <color>: Do you mean like this: every x tick label/.append style={text=\black}, ? Can you give an example if I am wrong?
    – phw
    Sep 13, 2016 at 9:31
  • 1
    for example, if you replace every x tick label/.append style={font=\color{black}} in preamble of axis with every x tick label/.append style={text=red}, the tick label will appear in red color and with font size as you declared in \pgfplotsset.
    – Zarko
    Sep 13, 2016 at 10:21
  • @Zarko: Is it possible that font=\color{black} is the whole reason for the problem? I just realised, when replacing it with text=black, everything works as it should ... and the every x tick label/.append style={text=black}, really appends instead of overwriting, what was one of my questions originally, when I asked if it is intended that the append command within the axis environment does actually not append but overwrite things... Where did you find that text=black is the right command for this purpose? I searched the pgfplots manual for text= and didn't find a single hit!
    – phw
    Sep 13, 2016 at 12:36
  • 1
    @phw: With the "Edit", "PS", and "Original Post" sections, this question has become very hard to read, both for someone who's been following the evolution of the question and especially for someone who comes across it for the first time. It's not necessary to keep the various versions visible in the post, they get stored in the post history. Could you edit the question so it becomes clear what is being asked? Or if you consider the problem solved, could you let us know so we can close the question?
    – Jake
    Sep 13, 2016 at 13:17

1 Answer 1

8

You can use the execute at begin axis key to sneak another \pgfplotsset command into the axis options. This \pgfplotsset command will be executed after the other axis options have been set:

\documentclass[12pt,a4paper,tikz]{standalone}
 \usepackage[T1]{fontenc}
 \usepackage[utf8]{inputenc}
 \usepackage{lmodern}
 \usepackage{tikz,pgfplots,grffile,amsmath}
 \pgfplotsset{compat=newest}
 \pgfplotsset{
    mytikzstyle/.style={
        every axis/.append style={
            execute at begin axis={
                \pgfplotsset{
                    legend style={
                        font=\tiny
                    },
                    every x tick label/.append style={font=\scriptsize},
                    every y tick label/.append style={font=\scriptsize}
                }
            }
        }
    }
 }
 \pgfplotsset{mytikzstyle}
 \pgfplotsset{/pgfplots/width=8cm}
 \pgfplotsset{/pgfplots/height=6cm}

 \begin{document}
 \begin{tikzpicture}
 \begin{axis}[%
 separate axis lines,
 every x tick label/.append style={font=\color{black}},
 legend style={at={(0.99,0.01)}, anchor=south east, legend cell align=left, align=left, draw=gray},
 ]
 \addplot [line width=0.8pt, color=black]
   table[row sep=crcr]{%
 1  1\\
 100    100\\
 };
 \addlegendentry{Hello $U_{1}\frac{xyz}{xyz}$}
 \end{axis}
 \end{tikzpicture}%
 \end{document}
5
  • 2
    Thanks Jake - I tested it and it basically works, but it is very strange: When I am replacing every x tick label/.append style={font=\tiny}, with every axis/.append style={font=\tiny},, which should set every font that is somewhat involved with an axis to \tiny it does again work on everything BUT on the x tick label, as this seems to get overwritten. When I am replacing the font=\color{black} by text=black as suggested by Zarko, is suddenly works. Is it possible that all this is only a bug triggered by font=\color{black}, which seems to not append but overwrite everything?
    – phw
    Sep 13, 2016 at 12:36
  • 1
    I was wondering the whole time, why two commands, which both use .append and both should affect different properities (one of these should affect the fontsize, the other should affect the fontcolor) can't work together... and I think with your and Zarkos help I found the true reason: matlab2tikz uses some bad code by using `font=\color{black}', which seems to be a command that does not give an error but obviously results in unintended behaviour, as it prevents commands from the pgfplotsset section to be appended...
    – phw
    Sep 13, 2016 at 12:40
  • 1
    @phw I guess, without having looked at details, that when you do stylename/.append style={font=...} twice, you get that stylename essentially does font=<something>,font=<something else>, and the latter of those two font settings take precedence, as font=.. itself does not work as append. Sep 13, 2016 at 12:47
  • 3
    @phw: I think Torbjørn nailed it: font=\color{black} overwrites font=\tiny, and yes, using \color{black} is bad practice in a TikZ context.
    – Jake
    Sep 13, 2016 at 13:10
  • @Jake I'm very happy that I have given +1 thus you are arrive to 200k. My compliments. :-)
    – Sebastiano
    Aug 18, 2019 at 8:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .