[Tex/LaTex] pgfplotsset append style is not appended to tikzpicture axis environment

matlab2tikzpgfplotstikz-pgftikz-styles

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.

Best Answer

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}