Define a variable and add 1 to it's value
Using \pgfmathtruncatemacro
makes the trick.
\documentclass{standalone}
\usepackage{amsmath,tikz}
\usetikzlibrary{arrows,calc}
\begin{document}
\begin{tikzpicture}
\def \currentYear {2016}
\def \n {4}
\foreach \i in {0,...,\n}
{
\pgfmathtruncatemacro{\nextYear}{\currentYear + \i}
\node (N) at (\i,\i) {\nextYear};
}
\end{tikzpicture}
\end{document}
Related videos on Youtube
Sam
Updated on August 01, 2022Comments
-
Sam over 1 year
I'm programming a calendar in
LaTeX
and as it should be used for my univertsity planning, I want to split it up in semesters. So on one page there are the months from September to February and on the second page, there will be the months March to August.To have a dynamic code which I don't have to touch after finishing the calandar, I'm defining all the variables that will be used in the preamble.
For example
\def\year{2017}
This variable is later called by the Tikz calendar. My problem is, that the version of the calendar spreads over two different years. My idea was to define a second variable
\def\nextyear
which is equal to\year + 1
. Unfortunately\def\nextyear{\year+1}
returns2017+1
instead of2018
...How can I add one year to my variable?
-
Steven B. Segletes almost 7 yearsYou need
\numexpr
to carry out the [integer] calculation and\the
to display the result as text. So try\edef\nextyear{\the\numexpr\year+1\relax}
maybe? -
Sam almost 7 yearsGreat! This works perfectly.
-
Steven B. Segletes almost 7 yearsJust remember,
\edef
will evaluate\nextyear
at the time of\edef
invocation, whereas if you use\def
, it will be evaluated at the time of\nextyear
invocation. -
egreg almost 7 yearsIt's a bad idea to do
\def\year{2017}
, as\year
is a predefined count register (holding the current year). -
Sam almost 7 years@egreg Could you please tell my why that's a bad idea?
-
egreg almost 7 years@Sam Because you're destroying an internal variable of TeX. Just use a different name for your variable. And, with LaTeX, always use
\newcommand
.
-