use a variable in tikz pgfplot xtick

I was drawing a square function using pgfplots. So I defined the period \T, the number of periods \nbT, the max value \E. I can use those variables in my axis preamble except in xtick, how is it?

My code:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}

\begin{document}

\def\E{5} %valeur max à définir
\def\T{2} %période à définir
\def\quartT{0.5} % quart de période à définir pour écrire l'abscisse littérale T/4 car les opérations semblent interdites dans extra x tick ou xstick d'ailleurs
\def\nbT{4} %nombre de période à définir

\begin{tikzpicture}

\begin{axis} [
scale=2,
title={Tension $u(t)$},
xlabel={$t$}, ylabel={$u(t)$}, xmin=0, xmax=(\T*\nbT)*1.09, ymin=-1.19*\E, ymax=1.19*\E,
width=190pt,axis x line=middle, axis y line=middle,
extra x ticks={\quartT},
extra x tick labels={$\frac{T}{4}$},
extra y ticks={-\E,0,\E},
extra y tick labels={$-E$,$0$,$E$},
]
\pgfplotsinvokeforeach{0,1,...,\nbT} {                     % boucle de tracé des barres horizontales
\draw [blue, thick] (#1*\T,\E) -- (#1*\T+\T/2,\E) ;
\draw [blue, thick] (#1*\T+\T/2,-\E) -- (#1*\T+\T,-\E) ;
} ;

\pgfplotsinvokeforeach{0,1,...,\nbT} {                     %boucle de tracé des barres verticales
\draw [blue, thick] (#1*\T,-\E) -- (#1*\T,\E) ;
\draw [blue, thick] (#1*\T+\T/2,-\E) -- (#1*\T+\T/2,\E) ;
} ;

\end{axis}

\end{tikzpicture}

\end{document}

• I Am new here, sorry to see how my code uploaded.... Nov 24 '17 at 17:27
• Select the code and hit Ctrl + K, or click the button marked {} above the text field, then it will be marked up properly. Nov 24 '17 at 17:29
• I don't quite understand what you're asking about though, what exactly do you want to change about your diagram? Nov 24 '17 at 17:33
• hi, thx for your help. I would like to be able to define "extra x ticks" with a calculated variable like 0.25*\T instead of having to declare an additional variable \quartT. It is weird beacause I can use calculated variables everywhere in my axis preambule but there...I guess this is how it works. Anyway. By the way, I found a better solution using the floor function: Nov 25 '17 at 18:30
• \addplot[ blue, %thick, domain=0:\T*\nbT, samples=1000*\T, ] {-2*\E*(floor(1-sin(360*x/\T))-0.5) }; Nov 25 '17 at 18:40

You can use \pgfmathsetmacro{\<macroname>}{<calculation>} to save the result of a calculation to a macro. Hence, you can use \pgfmathsetmacro\quartT{\T/4} to define \quartT.

While your alternative method to making the plot itself works, it's not very efficient. It takes noticeably longer time to generate the PDF when you use 1000 samples. Another approach might be to plot a similar expression but with samples only at every half period (starting at -T/4), with the plot type const plot mark mid. A const plot draws horizontal and vertical lines between the plot points, making it useful here.

Because of the choice of samples, there is no need for the floor function and various other numbers, the much simpler \E*(sin(360/\T*x)) is sufficient.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}

\begin{document}
\newcommand\E{5} %valeur max à définir
\newcommand\T{2} %période à définir
\pgfmathsetmacro\quartT{\T/4} % quart de période à définir pour écrire l'abscisse littérale T/4 car les opérations semblent interdites dans extra x tick ou xstick d'ailleurs
\newcommand\nbT{4} %nombre de période à définir

\begin{tikzpicture}

\begin{axis} [
scale=2,
title={Tension $u(t)$},
xlabel={$t$}, ylabel={$u(t)$},
xmin=0, xmax=(\T*\nbT)*1.09, ymin=-1.19*\E, ymax=1.19*\E,
width=190pt,axis x line=middle, axis y line=middle,
extra x ticks={\quartT},
extra x tick labels={$\frac{T}{4}$},
extra y ticks={-\E,0,\E},
extra y tick labels={$-E$,$0$,$E$},
]

\addplot [blue, thick, samples at={-\quartT,\quartT,...,\T*\nbT}, const plot mark mid] {\E*(sin(360/\T*x))};

\end{axis}
\end{tikzpicture}
\end{document}

• thx, clever indeed, this const plot option i didn't know about may be usefull in the future. Nov 26 '17 at 2:42
• @JeanLallemand I updated my answer, the plotted expression could be simplified a lot. Nov 26 '17 at 9:22