1

I have to draw a lot of graphs in a similar way.

I used to code them using TikZ like this example:

% first the axis
\draw[axis, ->] (0,0) -- (3.5,0) node[below] {$\text{Menge}\ x$};
\draw[axis, ->] (0,0) -- (0,3.5) node[left] {$\text{Rate}\ \frac{\dif{x}}{\dif{t}}$};
% Now the graph itself
\draw[domain=0:3, smooth, graph] plot ({\x},1.2)
\node[sloped, above] {$\frac{\dif{x}}{\dif{t}} = r$};
% write labels to the graph
\draw (0,1.2) node[left] {$r$};

As you can see, I have defined some style, in order to have the same look every time. This is the intended result; I am very pleased with the look.

simple TikZ graph example

TikZ is well capable to draw this simple graph, but as the examples evolve, it isn't any more. TeX.SE gave the hint, to use pgfplots instead.

This is the same example in pgfplots

\begin{tikzpicture}
  \begin{axis}
     \addplot[graph, domain=0:3] {1.2} ;
  \end{axis}
\end{tikzpicture}

And the resulting output:

Simple PGF-Plot example

(Don't mind the missing label on the graph, ...)

There will be a lot of similar example graphs, all of them shall display the quality of the function, not it quantitive values.

Therefore I want to define a set of styles in my preamble, to use them throughout the document and to be able, to change them in one place (if needed). This is the set of definitions (in my words)

  • no bounding box (as is default with pgfplots)
  • instead two axes, y-axis to the left, x-axis at the bottom
  • axis slightly thicker than the graph
  • axis labels at the end of the axis
  • no axis ticks
  • label font size \footnote\sffamily,
  • axis arrows in style LaTeX
  • graph drawn in smooth line

In order to achieve my goal, I should add the following to my code

\begin{tikzpicture}
  \begin{axis}[%
    axis x line = middle,
    axis y line = center,
    xmax=4,
    xlabel = {Menge $x$},
    ylabel = {Rate $\frac{\dif{x}}{\dif{t}}$},
    %% Definition, where to put the description of the axes.
    every axis x label/.style={%
      at={(ticklabel cs:0.9)},
      anchor=near ticklabel,
      font=\small\sffamily
    },
    every axis y label/.style={
      at={(ticklabel cs:0.9)},
      rotate=90,
      anchor=near ticklabel,
      font=\small\sffamily
    },
    ticks=none,
    ]
    \addplot[domain=0:3] {1.2} ;
  \end{axis}
\end{tikzpicture}

As you can see, this is quite a lot of work, and as I have to repeat it on lots of graphic codes, ...

Here is the graphical result

enter image description here

Still, the axis are to thin.

So, how do I define the correct global styles, that give the desired results?

Actually, I have in my preamble:

\tikzset{
  %% Achsen werden mit dicken Strichen gezeichnet.  Die Pfeile sollen
  %% den Typ LaTeX haben.
  axis/.style={thick, >=latex, ->, font=\sffamily},
  %% Die Graphenlinien sollen ganz normale Liniendicken haben.
  graph/.style={anchor=north west, smooth},
  %% and so on ...
}
\pgfplotsset{
    dnt axis/.style={
      axis lines=middle,
      axis line style={-Latex}, 
      xlabel style={at=(current axis.right of origin), anchor=north east, font=\footnotesize\sffamily},
      ylabel style={at=(current axis.above origin), anchor=north east,  font=\footnotesize\sffamily},
    }
}

But that didn't help with all items.

And finally, here is an MWE:

\documentclass{scrartcl}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[%
      %% Would love to define
      % examplegraph
      ]
      \addplot[domain=0:3] {1.2} ;
    \end{axis}
  \end{tikzpicture}
\end{document}
  • 2
    You can use every axis/.append style and every picture/.append style keys to be run at every axis and tikzpicture respectively. – percusse Apr 2 '18 at 9:55
  • @percusse thank you for the hint of ´/.append style´, I only tried ´/.append´ (i.e. without the " style".). – Jan Apr 2 '18 at 11:02
2

I wouldn't use every axis as mentioned in the comment below the question, but would create a real custom style (with its own name) and load it as first key/option in the axis environment. The advantage of this approach is, that you can create a lot of styles that you can load as needed. Also you can create styles that depend on other styles, etc.

In the following code you should rename the styles to something useful so that you/someone else already has an idea what the style is good for (like sketch style or something like that).

The only thing I wouldn't add to a (basic) style is the smooth option, because this can not be reverted, i.e. if -- for whatever reason -- you need a non-smooth line, you cannot use a style which includes the smooth option.

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usetikzlibrary{
        arrows.meta,
    }
    \pgfplotsset{
        compat=1.15,
        % create custom style and add all keys/options that are valid for this
        % particular style
        my axis style/.style={
            axis x line=middle,
            axis y line=middle,
            axis line style={
                thick,
                -Latex,
            },
            %% Definition, where to put the description of the axes.
            xlabel style={
                at={(ticklabel cs:1)},
                anchor=north east,
                font=\small\sffamily,
            },
            ylabel style={
                at={(ticklabel cs:1)},
                anchor=south east,
                rotate=90,
                font=\small\sffamily,
            },
            ticks=none,
        },
        % (this is for demonstration purposes only)
        % this is a style very similar to the previous one ...
        my similar axis style/.style={
            % ... so you can use the previous one as a basis
            my axis style,
            % and than add options that should be added to this style
            xlabel={Menge $x$},
            ylabel={Rate $\frac{d\,x}{d\,t}$},
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        % load custom style when needed/appropriate
        my similar axis style,
        % load keys/options that are only valid for this `axis' environment
        xmax=4,
    ]
        \addplot [domain=0:3] {1.2};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

  • 1
    You can also use prefix style/. it would lead to the same usage. Otherwise youhave to type this to every picture. – percusse Apr 2 '18 at 11:50
  • @percusse, sorry, but I am not sure what you mean. Could you elaborate a bit? – Stefan Pinnow Apr 2 '18 at 16:15
  • 1
    every axis/.prefix style={my similar axis style,} does what you have written. It places the custom style as the first thing. – percusse Apr 2 '18 at 16:49
  • 1
    @percusse, that is true, but that makes only sense -- if I have written in my text (hopefully) -- when really all axis environments should use this style. If not ... But nonetheless thanks for the comment. – Stefan Pinnow Apr 2 '18 at 17:03

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.