1

I want to have different plots styles. For example, I want a square plot and a plot with a larger width.

So actually I want to define global parameters

widthSquare = 8cm;

widthLarger = 10cm;

height = 8cm;

in order to create a tikzpicture which is using these parameters. The idea is that I can always change these variables without changing the tikz-Code of each plot and still have a consistent appearance of the plots.

Here is an example plot that uses widthSquare as a variable parameter and height.

\documentclass[a4paper, 11 pt]{article}
\usepackage{pgfplots}
\usepackage{tikz}

\pgfplotsset{compat=1.11}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
        % Dimensions of plot
        width = widthSquare,
        height = height,
        % Domain for values 
        xmin = 1,
        xmax = 20,
        ymin = 0,
        ymax = 250,
        % Axis labeling
        xlabel = {Frequency [Hz]},
        ylabel = {Amplitude},
        title = {Test plot},
        %legend style = {at={(1.05,0.95)}, anchor = north east, cells = {anchor = west}}
        ]
        \addplot [red, mark = none, thick, smooth] coordinates{(1,20)(5,60)(7,90)(17,150)};
        % Plot aus Datei data.txt mit Tabulator getrennten x und y Koordinaten in gleichen Ordner wie main
        %\addplot table {data.txt} oder auch {data.csv} möglich
        \legend{Graph1}
        \end{axis}
    \end{tikzpicture}
\end{document}
3
  • set default values in pgfplotsset in preamble
  • define new length \widthLarger and set its size
  • in exception, when you not use setted default values add to axis options width=\widthLarger

\documentclass[a4paper, 11 pt]{article}
\usepackage{pgfplots}
\usepackage{tikz}
    \newlength{\widthLarger}
    \setlength{\widthLarger}{10cm}
\pgfplotsset{compat=1.11,
    height=8cm,
    width=8cm}% most (standard) used width

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
        % Dimensions of plot
        % used default size determined in \pgfplotsset
        % Domain for values
        xmin = 1,
        xmax = 20,
        ymin = 0,
        ymax = 250,
        % Axis labeling
        xlabel = {Frequency [Hz]},
        ylabel = {Amplitude},
        title = {Test plot},
        %legend style = {at={(1.05,0.95)}, anchor = north east, cells = {anchor = west}}
        ]
        \addplot [red, mark = none, thick, smooth] coordinates{(1,20)(5,60)(7,90)(17,150)};
        % Plot aus Datei data.txt mit Tabulator getrennten x und y Koordinaten in gleichen Ordner wie main
        %\addplot table {data.txt} oder auch {data.csv} möglich
        \legend{Graph1}
        \end{axis}
    \end{tikzpicture}

    \begin{tikzpicture}
        \begin{axis}[
        % Dimensions of plot
        width = \widthLarger,
        % Domain for values
        xmin = 1,
        xmax = 20,
        ymin = 0,
        ymax = 250,
        % Axis labeling
        xlabel = {Frequency [Hz]},
        ylabel = {Amplitude},
        title = {Test plot},
        %legend style = {at={(1.05,0.95)}, anchor = north east, cells = {anchor = west}}
        ]
        \addplot [red, mark = none, thick, smooth] coordinates{(1,20)(5,60)(7,90)(17,150)};
        % Plot aus Datei data.txt mit Tabulator getrennten x und y Koordinaten in gleichen Ordner wie main
        %\addplot table {data.txt} oder auch {data.csv} möglich
        \legend{Graph1}
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

  • Thank you a lot for the great answer. Just an off-topic question. Why are many tex users not typesetting spaces before and after an equal sign? Is it just to save keystrokes or is there something more fundamental behind this convention? – MrYouMath Aug 20 '17 at 17:46
  • i don't know. merely mater of personal habit and taste :) – Zarko Aug 20 '17 at 18:33

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.