Define a variable and add 1 to it's value

3,123

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}

enter image description here

Share:
3,123

Related videos on Youtube

Sam
Author by

Sam

Updated on August 01, 2022

Comments

  • Sam
    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} returns 2017+1 instead of 2018...

    How can I add one year to my variable?

    • Steven B. Segletes
      Steven B. Segletes almost 7 years
      You 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
      Sam almost 7 years
      Great! This works perfectly.
    • Steven B. Segletes
      Steven B. Segletes almost 7 years
      Just 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
      egreg almost 7 years
      It's a bad idea to do \def\year{2017}, as \year is a predefined count register (holding the current year).
    • Sam
      Sam almost 7 years
      @egreg Could you please tell my why that's a bad idea?
    • egreg
      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.