%%
%% This is file `persian-calendar.sty',
%%
%%% Copyright © 2015-2026 by Javad Razavian
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%% This work has the LPPL maintenance status `author-maintained'.
%%
%% This work is “author-maintained” (as per LPPL maintenance status)
%% by Javad Razavian
%% The Current Maintainer of this work is Javad Razavian
%%
%% This work consists of the files persian-calendar.sty and persian-calendar-doc.tex.%%
%%
%%
\NeedsTeXFormat{LaTeX2e}
\def\pcalendardate{2026/11/06}
\def\pcalendarversion{0.78}
\ProvidesPackage{persian-calendar}[\pcalendardate\space v\pcalendarversion\space Provides Persian Calendar]
\RequirePackage{pgf}
\RequirePackage{pgfcalendar}
% Option `tikz' also loads TikZ and its calendar library, for the Jalali calendars and \pcalyear.
\newif\ifpcal@tikz
\DeclareOption{tikz}{\pcal@tikztrue}
\DeclareOption*{\PackageWarning{persian-calendar}{Unknown option `\CurrentOption' ignored}}
\ProcessOptions\relax
\ifpcal@tikz\RequirePackage{tikz}\usetikzlibrary{calendar}\fi
%
\newif\ifpcal@leap\newif\ifpcal@kabiseh%
\newcount\pcal@i%
\newcount\pcal@y%
\newcount\pcal@m%
\newcount\pcal@d%
\newcount\pcal@latini%
\newcount\pcal@latinii%
\newcount\pcal@latiniii%
\newcount\pcal@latiniv%
\newcount\pcal@latinv%
\newcount\pcal@latinvi%
\newcount\pcal@latinvii%
\newcount\pcal@latinviii%
\newcount\pcal@latinix%
\newcount\pcal@latinx%
\newcount\pcal@latinxi%
\newcount\pcal@latinxii%
%
\newcount\pcal@persiani%
\newcount\pcal@persianii%
\newcount\pcal@persianiii%
\newcount\pcal@persianiv%
\newcount\pcal@persianv%
\newcount\pcal@persianvi%
\newcount\pcal@persianvii%
\newcount\pcal@persianviii%
\newcount\pcal@persianix%
\newcount\pcal@persianx%
\newcount\pcal@persianxi%
\newcount\pcal@persianxii%
\newcount\pcal@persianxiii%
%
\newcount\pcal@temp%
\newcount\pcal@temptwo%
\newcount\pcal@tempthree%
\newcount\pcal@yModHundred%
\newcount\pcal@thirtytwo%
\newcount\pcal@dn%
\newcount\pcal@sn%
\newcount\pcal@mminusone%
\newcount\pcal@mod%
\newcount\pcal@loop%
\newcount\pcal@thousandth%
\newcount\pcal@decimal%
\newcount\pcal@first%
\newcount\pcal@second%
%
\def\pcal@getymd #1-#2-#3;{\def\pcal@get@y{#1}\def\pcal@get@m{#2}\def\pcal@get@d{#3}}%
%
\def\pcal@modulo#1#2{\number\numexpr#1-((#1/#2)*#2)\relax}%
%
\def\pcal@gregoriantojalali#1{%
    \pcal@getymd #1;
    \pcal@y=\pcal@get@y%year
    \pcal@m=\pcal@get@m%month
    \pcal@d=\pcal@get@d%day
    \pcal@temp=\pcal@y
    \divide\pcal@temp by 100\relax
    \multiply\pcal@temp by 100\relax
    \pcal@yModHundred=\pcal@y
    \advance\pcal@yModHundred by -\pcal@temp\relax
    \ifodd\pcal@yModHundred
        \pcal@leapfalse
    \else
    \pcal@temp=\pcal@yModHundred
    \divide\pcal@temp by 2\relax
    \ifodd\pcal@temp\pcal@leapfalse
    \else
        \ifnum\pcal@yModHundred=0%
            \pcal@temp=\pcal@y
            \divide\pcal@temp by 400\relax
            \multiply\pcal@temp by 400\relax
            \ifnum\pcal@y=\pcal@temp\pcal@leaptrue\else\pcal@leapfalse\fi
        \else\pcal@leaptrue
        \fi
    \fi
    \fi
    \pcal@latini=31\relax
    \ifpcal@leap
        \pcal@latinii = 29\relax
    \else
        \pcal@latinii = 28\relax
    \fi
    \pcal@latiniii = 31\relax
    \pcal@latiniv  = 30\relax
    \pcal@latinv = 31\relax
    \pcal@latinvi = 30\relax
    \pcal@latinvii = 31\relax
    \pcal@latinviii = 31\relax
    \pcal@latinix = 30\relax
    \pcal@latinx = 31\relax
    \pcal@latinxi = 30\relax
    \pcal@latinxii = 31\relax
    \pcal@thirtytwo=32\relax
    \pcal@temp=\pcal@y
    \advance\pcal@temp by -17\relax
    \pcal@temptwo=\pcal@temp
    \divide\pcal@temptwo by 33\relax
    \multiply\pcal@temptwo by 33\relax
    \advance\pcal@temp by -\pcal@temptwo
    \ifnum\pcal@temp=\pcal@thirtytwo\pcal@kabisehfalse
    \else
        \pcal@temptwo=\pcal@temp
        \divide\pcal@temptwo by 4\relax
        \multiply\pcal@temptwo by 4\relax
        \advance\pcal@temp by -\pcal@temptwo
        \ifnum\pcal@temp=\z@\pcal@kabisehtrue\else\pcal@kabisehfalse\fi
    \fi
    \pcal@tempthree=\pcal@y                 % Number of Leap years
    \advance\pcal@tempthree by -1
    \pcal@temp=\pcal@tempthree              % T := (MY-1) div 4
    \divide\pcal@temp by 4\relax
    \pcal@temptwo=\pcal@tempthree           % T := T - ((MY-1) div 100)
    \divide\pcal@temptwo by 100\relax
    \advance\pcal@temp by -\pcal@temptwo
    \pcal@temptwo=\pcal@tempthree           % T := T + ((MY-1) div 400)
    \divide\pcal@temptwo by 400\relax
    \advance\pcal@temp by \pcal@temptwo
    \advance\pcal@tempthree by -611       % Number of Kabise years
    \pcal@temptwo=\pcal@tempthree           % T := T - ((SY+10) div 33) * 8
    \divide\pcal@temptwo by 33\relax
    \multiply\pcal@temptwo by 8\relax
    \advance\pcal@temp by -\pcal@temptwo
    \pcal@temptwo=\pcal@tempthree           %
    \divide\pcal@temptwo by 33\relax
    \multiply\pcal@temptwo by 33\relax
    \advance\pcal@tempthree by -\pcal@temptwo
    \ifnum\pcal@tempthree=32\advance\pcal@temp by 1\fi % if (SY+10) mod 33=32 then Inc(T);
    \divide\pcal@tempthree by 4\relax     % T := T - ((SY+10) mod 33) div 4
    \advance\pcal@temp by -\pcal@tempthree
    \advance\pcal@temp by -137            % T := T - 137  Adjust the value
    \pcal@persiani=31
    \advance\pcal@persiani by -\pcal@temp                 % now 31 - T is the persiani
    \pcal@persianii = 30\relax
    \ifpcal@kabiseh
        \pcal@persianiii = 30\relax
    \else
        \pcal@persianiii = 29\relax
    \fi
    \pcal@persianiv  = 31\relax
    \pcal@persianv   = 31\relax
    \pcal@persianvi  = 31\relax
    \pcal@persianvii = 31\relax
    \pcal@persianviii= 31\relax
    \pcal@persianix  = 31\relax
    \pcal@persianx   = 30\relax
    \pcal@persianxi  = 30\relax
    \pcal@persianxii = 30\relax
    \pcal@persianxiii= 30\relax
    \pcal@dn= 0\relax
    \pcal@sn= 0\relax
    \pcal@mminusone=\pcal@m
    \advance\pcal@mminusone by -1\relax
    \pcal@i=0\relax
    \ifnum\pcal@i < \pcal@mminusone
    \loop
    \advance \pcal@i by 1\relax
    \advance\pcal@dn by \csname pcal@latin\romannumeral\the\pcal@i\endcsname
    \ifnum\pcal@i<\pcal@mminusone \repeat
    \fi
    \advance \pcal@dn by \pcal@d
    \pcal@i=1\relax
    \pcal@sn = \pcal@persiani
    \ifnum \pcal@sn<\pcal@dn
    \loop
    \advance \pcal@i by 1\relax
    \advance\pcal@sn by \csname pcal@persian\romannumeral\the\pcal@i\endcsname
    \ifnum \pcal@sn<\pcal@dn \repeat
    \fi
    \ifnum \pcal@i < 4
        \pcal@m = 9 \advance\pcal@m by \pcal@i
        \advance \pcal@y by -622\relax
    \else
        \pcal@m = \pcal@i \advance \pcal@m by -3\relax
        \advance \pcal@y by -621\relax
    \fi
    \advance\pcal@sn by -\csname pcal@persian\romannumeral\the\pcal@i%
    \endcsname
    \ifnum\pcal@i = 1
        \pcal@d = \pcal@dn \advance \pcal@d by 30 \advance\pcal@d by -\pcal@persiani
    \else
        \pcal@d = \pcal@dn \advance \pcal@d by -\pcal@sn
    \fi
}
%
\newcount\pcal@gy%
\newcount\pcal@gm%
\newcount\pcal@gd%
\newcount\pcal@days%
%
\def\pcal@jalalitogregorian#1{%
    \pcal@getymd #1;
    \pcal@y=\pcal@get@y% jalali year
    \pcal@m=\pcal@get@m% jalali month
    \pcal@d=\pcal@get@d% jalali day
    %
    \ifnum\pcal@y<980
        \pcal@gy=621\relax
    \else
        \pcal@gy=1600\relax
        \advance\pcal@y by -979\relax
    \fi
    \pcal@days=\pcal@y
    \multiply\pcal@days by 365\relax
    %
    \pcal@temp=\pcal@y
    \divide\pcal@temp by 33\relax
    \multiply\pcal@temp by 8\relax
    \advance\pcal@days by \pcal@temp
    %%%%%%%%%%%%%%%%%%%%%%%%%%%
    \pcal@temp=\pcal@y
    \divide\pcal@temp by 33\relax
    \pcal@temptwo=\pcal@temp
    \multiply\pcal@temptwo by 33\relax
    \pcal@temp=\pcal@y
    \advance\pcal@temp by -\pcal@temptwo
    %%%%%\pcal@temp=\pcal@modulo{\pcal@y}{33}
    \advance\pcal@temp by 3\relax
    \divide\pcal@temp by 4\relax
    \advance\pcal@days by \pcal@temp\relax
    \advance\pcal@days by 78\relax
    \advance\pcal@days by \pcal@d\relax
    \ifnum\pcal@m < 7
        \pcal@temp=\pcal@m
        \advance\pcal@temp by -1\relax
        \multiply\pcal@temp by 31\relax
    \else
        \pcal@temp=\pcal@m
        \advance\pcal@temp by -7\relax
        \multiply\pcal@temp by 30\relax
        \advance\pcal@days by 186\relax
    \fi
    \advance\pcal@days by \pcal@temp\relax
    %
    \pcal@temp=\pcal@days
    \divide\pcal@temp by 146097\relax
    \multiply\pcal@temp by 400\relax
    \advance\pcal@gy by \pcal@temp\relax
    %
    \pcal@temp=\pcal@days
    \divide\pcal@temp by 146097\relax
    \pcal@temptwo=\pcal@temp
    \multiply\pcal@temptwo by 146097\relax
    \advance\pcal@days by -\pcal@temptwo\relax
    %
    %\advance\pcal@days by -\pcal@modulo{\pcal@days}{146097}\relax
    \ifnum\pcal@days > 36524
        \advance\pcal@days by -1\relax
        \pcal@temp=\pcal@days
        \advance\pcal@temp by -1\relax
        \divide\pcal@temp by 36524\relax
        \multiply\pcal@temp by 100\relax
        \advance\pcal@gy by \pcal@temp
    %
        \pcal@temp=\pcal@days
        \divide\pcal@temp by 36524\relax
        \pcal@temptwo=\pcal@temp
        \multiply\pcal@temptwo by 36524\relax
        \advance\pcal@days by -\pcal@temptwo\relax
    %    \pcal@temp=\pcal@modulo{\pcal@days}{36524}
    %    \advance\pcal@days by -\pcal@temp\relax
        \ifnum\pcal@days>366
            \advance\pcal@days by 1\relax
        \fi
    \fi
    \pcal@temp=\pcal@days
    \divide\pcal@temp by 1461\relax
    \multiply\pcal@temp by 4\relax
    \advance\pcal@gy by \pcal@temp
    %
    \pcal@temp=\pcal@days
    \divide\pcal@temp by 1461\relax
    \pcal@temptwo=\pcal@temp
    \multiply\pcal@temptwo by 1461\relax
    \advance\pcal@days by -\pcal@temptwo\relax
    %
    %\pcal@temp=\pcal@modulo{\pcal@days}{1461}
    %\advance\pcal@days by -\pcal@temp\relax
    \pcal@temp=\pcal@days
    \advance\pcal@temp by -1\relax
    \divide\pcal@temp by 365\relax
    \advance\pcal@gy by \pcal@temp
    \ifnum\pcal@days > 365
    %
        \advance\pcal@days by -1\relax
        \pcal@temp=\pcal@days
        \divide\pcal@temp by 365\relax
        \pcal@temptwo=\pcal@temp
        \multiply\pcal@temptwo by 365\relax
        \advance\pcal@days by -\pcal@temptwo\relax
    %
    %    \pcal@temp=\pcal@modulo{\pcal@days}{365}
    %    \advance\pcal@days by -\pcal@temp\relax
    \fi
    \pcal@gd=\pcal@days
    \advance\pcal@gd by 1\relax
    \pcal@leapfalse
    \ifnum\pcal@modulo{\pcal@gy}{4}=0
        \pcal@mod=\pcal@modulo{\pcal@gy}{100}
        \ifnum\ifnum\pcal@mod>0 1\else\ifnum\pcal@mod<0 1\else 0\fi\fi=1
            \pcal@leaptrue
        \fi
    \fi
    \ifnum\pcal@modulo{\pcal@gy}{400}=0
        \pcal@leaptrue
    \fi
    \pcal@loop=1
    \loop
        \ifnum\pcal@gd>\pcal@greg@month{\pcal@loop}
        \advance\pcal@gd by -\pcal@greg@month{\pcal@loop}\relax
        \advance\pcal@loop by 1\relax
    \repeat
    \pcal@gm=\pcal@loop
}
\def\pcal@greg@month#1{%
    \ifcase#1\or 31\or%
    \ifpcal@leap 29\else 28\fi%
    \or 31\or 30\or 31\or 30\or 31\or 31\or 30\or 31\or 30\or 31%
    \fi
}
%
\def\pdate#1{%
    \pcal@gregoriantojalali{#1}
    \number\pcal@d\space%
    \pcal@persian@month{\pcal@m}\space\number\pcal@y%
}
%
\def\gdate#1{%
    \pcal@jalalitogregorian{#1}
    \number\pcal@gd\space%
    \pcal@gregorian@month{\pcal@gm}\space\number\pcal@gy%
}
%
\def\pcalendar@unpack#1+{\count8=#1\relax}%
\def\pcaldatetojulian#1#2{\edef\pcal@arg{#1}\expandafter\pcalendar@datetojulian\pcal@arg+/{#2}}
\def\pcalendar@datetojulian#1-#2-#3+#4/#5{%
    \count1=#1\relax% % jalali year
    \count2=#2\relax% % jalali month
    \edef\pcal@arg{#3}% % jalali day
    \count3=\pcal@arg\relax%
    \edef\pcal@@temp{#4}%
    \ifx\pcal@@temp\pgfutil@empty%
        \count8=0\relax%
    \else%
        \pcalendar@unpack#4%
    \fi%
    \pcal@jalalitogregorian{\count1-\count2-\count3}
    \pgfcalendardatetojulian{\pcal@gy-\pcal@gm-\pcal@gd}{#5}
    \advance#5 by \count8\relax
}%
%
\def\pcaljuliantodate#1#2#3#4{%
    \pgfcalendarjuliantodate#1#2#3#4
    \pcal@gregoriantojalali{#2-#3-#4}
    \let#2=\pcal@y
    \let#3=\pcal@m
    \let#4=\pcal@d
}%
%
% Weekday of a Jalali date (Y-M-D, optionally +offset) into count #2, Saturday = 0, as \pcalweekdayname.
% pgfcalendar counts from Monday = 0, hence the shift by two.
\def\pcalweekday#1#2{\pcaldatetojulian{#1}{\pcal@temp}\pgfcalendarjuliantoweekday{\pcal@temp}{#2}%
    \advance#2 by 2 \ifnum#2>6 \advance#2 by -7 \fi}%
\def\pcaldateweekdayname#1{{\pcalweekday{#1}{\pcal@temptwo}\pcalweekdayname{\pcal@temptwo}}}%
% Days from Jalali date #1 to Jalali date #2 (each Y-M-D, optionally +offset) into count #3; negative if #2 is earlier.
% #1 goes into #3 first: the second conversion uses \pcal@temp but not #3.
\def\pcaldaysbetween#1#2#3{\pcaldatetojulian{#1}{#3}\pcaldatetojulian{#2}{\pcal@temp}%
    \multiply#3 by -1 \advance#3 by \pcal@temp}%
%
\def\pisodate#1{%
    \pcal@gregoriantojalali{#1}
    \ifnum\pcal@d<10 0\fi\number\pcal@d\slash%
    \ifnum\pcal@m<10 0\fi\number\pcal@m\slash%
    \number\pcal@y%
}%
\def\gisodate#1{%
    \pcal@jalalitogregorian{#1}%
    \ifnum\pcal@gd<10 0\fi\number\pcal@gd\slash%
    \ifnum\pcal@gm<10 0\fi\number\pcal@gm\slash%
    \number\pcal@gy%
}%
%
\def\pcalyearname#1{%
\ifnum#1>-1\ifnum#1<2100\relax
    \pcal@thousandth=#1
    \pcal@decimal=#1
    \divide\pcal@thousandth by 100\relax
    \pcal@temp=\pcal@thousandth
    \multiply\pcal@temp by 100\relax
    \advance\pcal@decimal by -\pcal@temp\relax
%
    \pcal@first=\pcal@thousandth
    \pcal@temp=\pcal@thousandth
    \divide\pcal@temp by 3\relax
    \multiply\pcal@temp by 3\relax
    \advance\pcal@first by -\pcal@temp\relax
%
    \pcal@second=\pcal@decimal
    \pcal@temp=\pcal@decimal
    \divide\pcal@temp by 12\relax
    \multiply\pcal@temp by 12\relax
    \advance\pcal@second by -\pcal@temp\relax
    \ifcase\pcal@first
        \ifcase\pcal@second
        مار\or اسب\or گوسفند\or میمون\or مرغ\or سگ\or خوک\or موش\or گاو\or پلنگ\or خرگوش\or نهنگ\fi\or
        \ifcase\pcal@second
        مرغ\or سگ\or خوک\or موش\or گاو\or پلنگ\or خرگوش\or نهنگ\or مار\or اسب\or گوسفند\or میمون\fi\or
        \ifcase\pcal@second
        گاو\or پلنگ\or خرگوش\or نهنگ\or مار\or اسب\or گوسفند\or میمون\or مرغ\or سگ\or خوک\or موش\fi
    \fi
\fi\fi
}%
%
\def\pcal@persian@month#1{\ifcase#1\or فروردین\or اردیبهشت\or خرداد\or تیر\or مرداد\or شهریور%
\or مهر\or آبان\or آذر\or دی\or بهمن\or اسفند\fi}%
\def\pcal@gregorian@month#1{\ifcase#1\or ژانویه\or فوریه\or مارس\or آوریل\or می\or ژوئن%
\or جولای\or آگوست\or سپتامبر\or اکتبر\or نوامبر\or دسامبر\fi}%
%
\def\pcalweekdayname#1{\ifcase#1 شنبه\or یکشنبه\or دوشنبه\or سه‌شنبه\or چهارشنبه\or پنجشنبه\or جمعه\fi}%
\def\pcalweekdayshortname#1{\ifcase#1 ش\or ی\or د\or س\or چ\or پ\or ج\fi}%
\def\pcalmonthname#1{\pcal@persian@month{#1}}%
\def\pcalgregorianmonthname#1{\pcal@gregorian@month{#1}}%
%
% ---- Jalali calendars with the TikZ `calendar' library ----
% Grouped because the converters use \loop and pgfcalendar runs its days in a \loop.
\def\pcal@tojalali#1#2#3{\begingroup\pcal@gregoriantojalali{#1-#2-#3}%
    \xdef\pcal@jy{\the\pcal@y}\xdef\pcal@jm{\the\pcal@m}\xdef\pcal@jd{\the\pcal@d}\endgroup}%
\def\pcal@togregorian#1{\begingroup\pcal@jalalitogregorian{#1}%
    \xdef\pcal@iso{\the\pcal@gy-\the\pcal@gm-\the\pcal@gd}\endgroup}%
% Jalali date of the day being drawn; weekday counts from Saturday = 0, like \pcalweekdayname.
\def\pcal@current{%
    \pcal@tojalali\pgfcalendarcurrentyear\pgfcalendarcurrentmonth\pgfcalendarcurrentday
    \let\pcalcurrentyear\pcal@jy\let\pcalcurrentmonth\pcal@jm\let\pcalcurrentday\pcal@jd
    \pcal@temp=\pgfcalendarcurrentweekday\advance\pcal@temp by 2\relax
    \ifnum\pcal@temp>6 \advance\pcal@temp by -7\relax\fi
    \edef\pcalcurrentweekday{\the\pcal@temp}%
    \let\pcalendarcurrentyear\pcalcurrentyear\let\pcalendarcurrentmonth\pcalcurrentmonth % names before 0.71
    \let\pcalendarcurrentday\pcalcurrentday\let\pcalendarcurrentweekday\pcalcurrentweekday}%
\def\pcal@iflast#1{\pgfutil@in@{last}{#1}}%
\def\pcal@ifdatejalali{\pcal@tojalali\pgfcalendarifdateyear\pgfcalendarifdatemonth\pgfcalendarifdateday}%
\newif\ifpcal@boxes \newif\ifpcal@initials \newif\ifpcal@columns
% Height and depth of #1 in the node font (TikZ draws in \nullfont), into \pcal@txtht and \pcal@txtdp.
\def\pcal@measure#1{\pgfinterruptpicture\tikz@textfont\global\setbox\pgfutil@tempboxa\hbox{#1}\endpgfinterruptpicture
    \xdef\pcal@txtht{\the\ht\pgfutil@tempboxa}\xdef\pcal@txtdp{\the\dp\pgfutil@tempboxa}}%
\def\pcal@digits{0123456789}%
\def\pcal@initialsample{\pcalweekdayshortname{0}\pcalweekdayshortname{1}\pcalweekdayshortname{2}%
    \pcalweekdayshortname{3}\pcalweekdayshortname{4}\pcalweekdayshortname{5}\pcalweekdayshortname{6}}%
% One cell: #1 node options, #2 text, #3 sample text whose height and depth the cell's text gets.
% A box is one day step wide and high, its right edge on the day's position like a plain day. All boxes
% share one centre line (where digits on the baseline are centred), so the rows touch; the text of each
% kind (digits, initials) is centred in its box, so tall or deep letters stay off the borders.
\def\pcal@cell#1#2#3{\ifpcal@boxes
    \pcal@measure{\pcal@digits}\pgf@ya=\pcal@txtht\advance\pgf@ya by-\pcal@txtdp\pgf@ya=.5\pgf@ya
    \edef\pcal@mid{\the\pgf@ya}\pcal@measure{#3}%
    \node[every day,draw,anchor=east,inner sep=0pt,text height=\pcal@txtht,text depth=\pcal@txtdp,
      minimum width={abs(\tikz@lib@cal@xshift)},minimum height={abs(\tikz@lib@cal@yshift)},#1]
      at (0pt,\pcal@mid) {#2};%
  \else\node[every day,#1]{#2};\fi}%
% Drop to a new month: at least three rows (four with initials) so the label and the initials fit
% below an unfinished last week; only with boxes or initials, so plain lists keep `month yshift'.
\def\pcal@nextmonth{\pgfmathsetlength{\pgf@y}{\tikz@lib@cal@month@yshift}%
    \ifpcal@boxes\pcal@roomfornext\else\ifpcal@initials\pcal@roomfornext\fi\fi
    \pgftransformyshift{-\pgf@y}}%
\def\pcal@roomfornext{\pgfmathsetlength{\pgf@ya}{\tikz@lib@cal@yshift}%
    \ifpcal@initials\pgf@ya=4\pgf@ya\else\pgf@ya=3\pgf@ya\fi
    \ifdim\pgf@y<\pgf@ya\pgf@y=\pgf@ya\fi}%
% Empty boxes before the first day and after the last day of a month (or of the calendar's range),
% so every month is a full rectangle. Runs at the start of the row, before the day is placed.
\newcount\pcal@lo \newcount\pcal@hi \newif\ifpcal@last
\def\pcal@pad{\pcal@steps
    \pcal@lo=0 \pcal@hi=\pcalcurrentweekday\advance\pcal@hi by -1
    \ifnum\pcalcurrentday=1 \pcal@padrow\else
      \ifnum\pgfcalendarcurrentjulian=\pgfcalendarbeginjulian\relax\pcal@padrow\fi\fi
    \pcal@checklast
    \ifpcal@last\pcal@lo=\pcalcurrentweekday\advance\pcal@lo by 1 \pcal@hi=6 \pcal@padrow\fi}%
\def\pcal@padrow{\pcal@padcell0\pcal@padcell1\pcal@padcell2\pcal@padcell3\pcal@padcell4\pcal@padcell5\pcal@padcell6}%
\def\pcal@padcell#1{\ifnum#1<\pcal@lo\else\ifnum#1>\pcal@hi\else
    {\pcal@moveto{#1}\pcal@cell{}{}{\pcal@digits}}\fi\fi}%
% Day steps, and a move to weekday #1 of the current week: along the row, or down the column.
\def\pcal@steps{\pgfmathsetlength{\pgf@x}{\tikz@lib@cal@xshift}\edef\pcal@step{\the\pgf@x}%
    \pgfmathsetlength{\pgf@y}{\tikz@lib@cal@yshift}\edef\pcal@ystep{\the\pgf@y}}%
\def\pcal@moveto#1{\ifpcal@columns\pgf@y=-#1\dimexpr\pcal@ystep\relax\pgftransformyshift{\pgf@y}%
    \else\pgf@x=#1\dimexpr\pcal@step\relax\pgftransformxshift{\pgf@x}\fi}%
% \ifpcal@last: the day is the last of its Jalali month (tomorrow is the 1st) or of the calendar.
\def\pcal@checklast{\pcal@lastfalse
    \ifnum\pgfcalendarcurrentjulian=\pgfcalendarendjulian\relax\pcal@lasttrue\else
      {\pgfutil@tempcnta=\pgfcalendarcurrentjulian\advance\pgfutil@tempcnta by 1
       \pgfcalendarjuliantodate{\pgfutil@tempcnta}\pcal@ny\pcal@nm\pcal@nd
       \pcal@tojalali\pcal@ny\pcal@nm\pcal@nd\ifnum\pcal@jd=1 \global\pcal@lasttrue\fi}\fi}%
% The row of initials, one day step above the first week, Saturday in column 0.
\def\pcal@initials{{\pcal@steps
    \ifpcal@columns\pgf@x=-\pcal@step\pgftransformxshift{\pgf@x}\else\pgftransformyshift{\pcal@ystep}\fi
    \pcal@initial0\pcal@initial1\pcal@initial2\pcal@initial3\pcal@initial4\pcal@initial5\pcal@initial6}}%
\def\pcal@initial#1{{\pcal@moveto{#1}\pcal@cell{every jalali weekday}{\pcalweekdayshortname{#1}}{\pcal@initialsample}}}%
% For `jalali week columns': on the first of a month, the number of week columns it needs.
\def\pcal@countcolumns{{\pcal@temp=\pcalcurrentmonth\pcal@temptwo=\pcalcurrentyear
    \ifnum\pcal@temp=12 \pcal@temp=1 \advance\pcal@temptwo by 1 \else\advance\pcal@temp by 1 \fi
    \edef\pcal@arg{\the\pcal@temptwo-\the\pcal@temp-1}\expandafter\pcal@togregorian\expandafter{\pcal@arg}%
    \pgfcalendardatetojulian{\pcal@iso}{\pgfutil@tempcnta}% first day of the next month
    \advance\pgfutil@tempcnta by-\pgfcalendarcurrentjulian % days in this month
    \advance\pgfutil@tempcnta by\pcalcurrentweekday\advance\pgfutil@tempcnta by 6
    \divide\pgfutil@tempcnta by 7 \xdef\tikz@lib@cal@width{\the\pgfutil@tempcnta}}}%
% A new month in `jalali week columns': a new column if the last week was unfinished, then month xshift.
\def\pcal@nextmonthcolumn{\pgfmathsetlength{\pgf@x}{\tikz@lib@cal@xshift}%
    \pgfmathsetlength{\pgf@xa}{\tikz@lib@cal@month@xshift}\ifdim\pgf@x<0pt \pgf@xa=-\pgf@xa\fi
    \ifnum\pcalcurrentweekday>0 \advance\pgf@xa by\pgf@x\fi
    \pgftransformxshift{\pgf@xa}}%
\pgfkeys{%
  /pgf/calendar/jalali day of month/.code={\pcal@ifdatejalali\ifnum#1=\pcal@jd\relax\pgfcalendarmatchestrue\fi},
  /pgf/calendar/jalali equals/.code args={#1-#2}{\pcal@ifdatejalali
    \ifnum#1=\pcal@jm\relax\ifnum#2=\pcal@jd\relax\pgfcalendarmatchestrue\fi\fi},
  % jalali dates=Y-M-D to Y-M-D; the day may be `last'
  /tikz/jalali dates/.code args={#1to#2}{\pcal@togregorian{#1}\let\pcal@start\pcal@iso
    \pcal@getymd#2;\expandafter\pcal@iflast\expandafter{\pcal@get@d}%
    \ifpgfutil@in@ % first day of the next month, minus one
      \pcal@temp=\pcal@get@m\relax\pcal@temptwo=\pcal@get@y\relax
      \ifnum\pcal@temp=12 \pcal@temp=1 \advance\pcal@temptwo by 1 \else\advance\pcal@temp by 1 \fi
      \edef\pcal@arg{\the\pcal@temptwo-\the\pcal@temp-1}\expandafter\pcal@togregorian\expandafter{\pcal@arg}%
      \edef\pcal@iso{\pcal@iso+-1}%
    \else\pcal@togregorian{#2}\fi
    \pgfkeysalso{/tikz/dates/.expanded=\pcal@start to\pcal@iso}},
  % Prepended so \pcalcurrent... are set before any other per-day code.
  /tikz/jalali/.code={\expandafter\def\expandafter\tikz@before@day\expandafter{\expandafter\pcal@current\tikz@before@day}%
    \pgfkeysalso{/pgf/calendar/day of month/.code={\pgfkeysalso{/pgf/calendar/jalali day of month=##1}},
      /tikz/day text={\pcalcurrentday},
      /tikz/month text={\pcalmonthname{\pcalcurrentmonth}\space\pcalcurrentyear}}},
  % Like TikZ's `week list', but weeks run Saturday to Friday. Use a negative `day xshift' for right to left.
  /tikz/jalali week list/.style={jalali,
    execute before day scope={\ifdate{day of month=1}{\ifdate{equals=\pgfcalendarbeginiso}{}{\pcal@nextmonth}%
      \ifpcal@initials\pcal@initials\fi}{}%
      \ifpcal@boxes\pcal@pad\fi},
    execute at begin day scope={\pgfmathsetlength\pgf@x{\tikz@lib@cal@xshift}%
      \pgf@x=\pcalcurrentweekday\pgf@x\pgftransformxshift{\pgf@x}},
    execute after day scope={\ifdate{Friday}{\pgfmathsetlength{\pgf@y}{\tikz@lib@cal@yshift}\pgftransformyshift{-\pgf@y}}{}},
    tikz@lib@cal@width=7},
  % Weeks as columns, as in calxxxx-yyyy: one weekday per row, Saturday on top, a new column after Friday.
  % Use a negative `day xshift' for right to left.
  /tikz/jalali week columns/.code={\pcal@columnstrue\pgfkeysalso{/tikz/jalali week columns@}},
  /tikz/jalali week columns@/.style={jalali,
    execute before day scope={\ifdate{day of month=1}{\ifdate{equals=\pgfcalendarbeginiso}{}{\pcal@nextmonthcolumn}%
      \pcal@countcolumns\ifpcal@initials\pcal@initials\fi}{}%
      \ifpcal@boxes\pcal@pad\fi},
    execute at begin day scope={\pgfmathsetlength\pgf@y{\tikz@lib@cal@yshift}%
      \pgf@y=-\pcalcurrentweekday\pgf@y\pgftransformyshift{\pgf@y}},
    execute after day scope={\ifdate{Friday}{\pgfmathsetlength{\pgf@x}{\tikz@lib@cal@xshift}\pgftransformxshift{\pgf@x}}{}}},
  % Each day is a box one day step wide and high, so the boxes join into a grid.
  /tikz/jalali day boxes/.code={\pcal@boxestrue
    \pgfkeysalso{/tikz/day code={\pcal@cell{name=\pgfcalendarsuggestedname}{\tikzdaytext}{\pcal@digits}}}},
  % A row of weekday initials above each month (with `jalali week list').
  /tikz/jalali weekday initials/.code={\pcal@initialstrue},
  /tikz/every jalali weekday/.style={},
  % Like TikZ's `month label above centered', but centred on the days: the shift is half the width of
  % `30' in the current font plus the inner sep of a day node, not a fixed 1.5ex, so it follows the font size.
  /tikz/jalali month label above centered/.style={
    execute before day scope={\ifdate{day of month=1}{{%
      \pgfmathsetlength{\pgf@xa}{\tikz@lib@cal@xshift}%
      \pgf@xb=\tikz@lib@cal@width\pgf@xa\advance\pgf@xb by-\pgf@xa\pgf@xb=.5\pgf@xb
      \ifpcal@boxes % a box is one step wide and ends at the day's position
        \ifdim\pgf@xa<0pt \advance\pgf@xb by.5\pgf@xa\else\advance\pgf@xb by-.5\pgf@xa\fi
      \else
        \pgfinterruptpicture\tikz@textfont\global\setbox\pgfutil@tempboxa\hbox{30}\endpgfinterruptpicture % TikZ draws in \nullfont
        \advance\pgf@xb by-.5\wd\pgfutil@tempboxa
        \pgfmathsetlength{\pgf@xa}{\pgfkeysvalueof{/pgf/inner xsep}}\advance\pgf@xb by-\pgf@xa
      \fi
      \pgftransformxshift{\pgf@xb}%
      \pgfmathsetlength{\pgf@y}{\tikz@lib@cal@yshift}\pgf@ya=1.25\pgf@y
      \ifpcal@initials\ifpcal@columns\else\advance\pgf@ya by\pgf@y\fi\fi % above the row of initials
      \ifpcal@boxes\advance\pgf@ya by.25\pgf@y\fi % clear of the box tops
      \pgftransformyshift{\pgf@ya}%
      \tikzmonthcode}}{}},
    every month/.append style={anchor=base}}}%
%
% ---- \pcalyear{year}, \pcalyears{first}{last}: year calendars in the manner of calxxxx-yyyy ----
% A year is four rows of seasons, three months each, right to left; a month is a column per week and a
% row per weekday, with the initials beside it. Sized for nine years to an A4 page (0.3cm per day).
% Needs TikZ and its calendar library, which the package does not load.
\pgfkeys{/tikz/every pcal year/.style={}, /tikz/every pcal year title/.style={},
  /tikz/every pcal month/.style={}, /tikz/pcal friday/.style={red}}%
\def\pcal@needtikz{\ifx\tikz@lib@cal@calendar\@undefined\PackageError{persian-calendar}%
    {\string\pcalyear\space and \string\pcalyears\space need TikZ and its calendar library}%
    {Load the package with \string\usepackage[tikz]{persian-calendar}, or add \string\usepackage{tikz}
     and \string\usetikzlibrary{calendar} to the preamble.}\fi}%
\def\pcalyear#1{\pcal@needtikz\ifx\tikz@lib@cal@calendar\@undefined\else
  \begin{tikzpicture}[font=\tiny,every pcal year]
    \node[anchor=base east,font=\footnotesize\bfseries,every pcal year title] at (0.3,0.35) {#1};
    \foreach \pcal@mo in {1,...,12} {%
      \pgfmathtruncatemacro\pcal@col{mod(\pcal@mo-1,3)}% month within the season
      \pgfmathtruncatemacro\pcal@row{div(\pcal@mo-1,3)}% season
      \calendar at (-\pcal@col*2.15cm,-0.55cm-\pcal@row*2.1cm)
        [jalali dates=#1-\pcal@mo-01 to #1-\pcal@mo-last, jalali week columns, jalali weekday initials,
         day xshift=-0.3cm, day yshift=0.235cm, jalali month label above centered,
         month text={\pcalmonthname{\pcalcurrentmonth}},
         every jalali weekday/.style={gray}, every pcal month]
        if (Friday) [pcal friday];
    }
  \end{tikzpicture}\fi}%
% Three years to a row; a new row starts only when another year follows, so no empty paragraph is left.
\def\pcalyears#1#2{\par\noindent
  \foreach \pcal@yr [count=\pcal@yi] in {#1,...,#2} {%
    \ifnum\pcal@yi>1 \pgfmathtruncatemacro\pcal@yn{mod(\pcal@yi-1,3)}%
      \ifnum\pcal@yn=0 \par\bigskip\noindent\else\hfill\fi\fi
    \expandafter\pcalyear\expandafter{\pcal@yr}}\par}%
%
% ---- \pcalplanner{year}: a vertical year planner in the manner of yplan ----
% Two pages, six Jalali months each: a column per month, a row per day, rows aligned on the weekday
% (Saturday first), a double rule after each Friday. Built with recursion, not \loop (the converters use \loop).
\newcount\pcal@yprow \newcount\pcal@yprows \newcount\pcal@ypwd \newtoks\pcal@yptoks \newdimen\pcal@ypwidth
\def\pcalplannerdaystrut{\rule{0pt}{12pt}}%
\def\pcalplannermonthstrut{\rule{0pt}{12pt}}%
\def\pcalplannerleapyear{سال کبیسه}%
% Typesets one day cell: Jalali year, month, day, weekday (Saturday = 0). Redefine to colour or annotate days.
\def\pcalplannercell#1#2#3#4{#3}%
% Week numbers (on by default, off with \pcalplanner*). There is no official rule for numbering the weeks
% of the Jalali year, so this is ISO 8601 moved to the Persian week. ISO counts Monday-to-Sunday weeks and
% makes week 1 the week holding the year's first Thursday, the middle (4th) day of its week: the first week
% with at least four days in the new year. A Persian week runs Saturday to Friday and its 4th day is
% Tuesday, so here week 1 is the week holding the first Tuesday of the year, that is the week holding
% 4 Farvardin. As in ISO:
%  - every week is a full week, Saturday to Friday;
%  - a year has 52 weeks, or 53 when it has 53 Tuesdays: when 1 Farvardin is a Tuesday, or a Monday in a
%    leap year (ISO: a Thursday, or a Wednesday in a leap year);
%  - 1-3 Farvardin belong to the last week (52 or 53) of the old year when 1 Farvardin is a Wednesday,
%    Thursday or Friday, and the last days of Esfand to week 1 of the new year when the next 1 Farvardin
%    is a Sunday, Monday or Tuesday.
% The number goes in the Saturday cells, as calendars that follow ISO put it on Mondays, and also on
% 1 Farvardin when that is not a Saturday, since the Saturday of its week is in the previous year.
\newif\ifpcal@ypweeks
\def\pcalplannerweeknumber#1{{\scriptsize#1}}%
% Day of the year of Jalali month #1, day #2: six months of 31 days, then months of 30.
\def\pcal@ypyday#1#2{\ifnum#1<7 \numexpr(#1-1)*31+#2\relax\else\numexpr186+(#1-7)*30+#2\relax\fi}%
% Weeks in a year whose 1 Farvardin has weekday #1 (Saturday = 0) and which has #2 days: its Tuesdays.
\def\pcal@ypweeksin#1#2{\ifnum#1=3 53\else\ifnum#2=366 \ifnum#1=2 53\else52\fi\else52\fi\fi}%
% Weekday (Saturday = 0) of Julian day #1 into macro #2.
\def\pcal@ypwdof#1#2{{\pgfcalendarjuliantoweekday{#1}{\pcal@temp}\advance\pcal@temp by 2
    \ifnum\pcal@temp>6 \advance\pcal@temp by -7 \fi\xdef#2{\the\pcal@temp}}}%
% For year #1: \pcal@yps, the day of the year of the Saturday that starts week 1, minus one (from -3 to 3);
% \pcal@ypwn and \pcal@ypwp, the number of weeks of this year and of the previous one.
\def\pcal@ypweeksetup#1{\pcal@ypfirst{#1}{1}{a}\pcal@ypfirst{#1}{13}{f}%
    \edef\pcal@arg{\the\numexpr#1-1\relax}\pcal@ypfirst{\pcal@arg}{1}{z}%
    \pcal@ypwdof\pcal@ypja\pcal@ypwa \pcal@ypwdof\pcal@ypjz\pcal@ypwz
    % week 1 starts on the Saturday on or before 1 Farvardin if that is Saturday to Tuesday, else after it
    \edef\pcal@yps{\ifnum\pcal@ypwa<4 -\fi\the\numexpr\ifnum\pcal@ypwa<4 \pcal@ypwa\else7-\pcal@ypwa\fi\relax}%
    \edef\pcal@ypwn{\pcal@ypweeksin\pcal@ypwa{\the\numexpr\pcal@ypjf-\pcal@ypja\relax}}%
    \edef\pcal@ypwp{\pcal@ypweeksin\pcal@ypwz{\the\numexpr\pcal@ypja-\pcal@ypjz\relax}}}%
% Week number of month #1, day #2. x = days from the start of week 1; (x-3)/7 is floor(x/7) for x >= 0,
% since \numexpr rounds. Before week 1: the last week of the previous year; past the last week: week 1.
\def\pcal@ypweekno#1#2{\pcal@ypweekfix{\numexpr\pcal@ypyday{#1}{#2}-1-(\pcal@yps)\relax}}%
\def\pcal@ypweekfix#1{\ifnum#1<0 \pcal@ypwp\else
    \ifnum\numexpr(#1-3)/7+1\relax>\pcal@ypwn\space 1\else\the\numexpr(#1-3)/7+1\relax\fi\fi}%
\def\pcal@ypweekcell#1{\hfill\pcalplannerweeknumber{#1}}%
%
% Notes, as on a wall calendar: \pcalplannernote{M-D}{text} (every year) or \pcalplannernote{Y-M-D}{text}
% (that year only) puts \pcalplannermark after the day in its cell and lists the notes under the table of
% the page that holds the day, in date order, grouped by month: each month once, then its days, e.g.
% «فروردین: ۱) ... ۱۳) ...   خرداد: ۱۴) ...». A day with several notes is listed once, its texts joined
% by \pcalplannernotetextsep; notes for every year come first. Each note is stored as \pcal@n{text} in
% \pcal@ypn@M@D or \pcal@ypn@Y@M@D.
\def\pcalplannermark{\textsuperscript{*}}%
\def\pcalplannernotesfont{\footnotesize}%
% Heading of a month in the list (Jalali year, month), the label of a day (year, month, day), and the
% separators between months, between days and between the texts of one day.
\def\pcalplannernotemonth#1#2{\textbf{\pcalmonthname{#2}:}~}%
\def\pcalplannernoteday#1#2#3{#3)~}%
\def\pcalplannernotemonthsep{\qquad}%
\def\pcalplannernotesep{\quad}%
\def\pcalplannernotetextsep{،~}%
\def\pcalplannernote#1#2{\pcal@npar#1---\@nil{#2}}%
\def\pcal@npar#1-#2-#3-#4\@nil#5{\def\pcal@arg{#3}%
    \ifx\pcal@arg\pgfutil@empty\edef\pcal@nkey{pcal@ypn@\number#1@\number#2}%
    \else\edef\pcal@nkey{pcal@ypn@\number#1@\number#2@\number#3}\fi
    \expandafter\ifx\csname\pcal@nkey\endcsname\relax\expandafter\gdef\csname\pcal@nkey\endcsname{}\fi
    \expandafter\g@addto@macro\csname\pcal@nkey\endcsname{\pcal@n{#5}}}%
% Expands to \pcalplannermark if month #1, day #2 of the planner's year has a note.
\def\pcal@ypnotemark#1#2{\ifnum\ifcsname pcal@ypn@#1@#2\endcsname1\else
    \ifcsname pcal@ypn@\pcal@ypyear @#1@#2\endcsname1\else0\fi\fi=1 \noexpand\pcalplannermark\fi}%
% The notes of the page's six months, walked day by day (by recursion: the converters use \loop).
% Flags: no note yet on the page, none yet in this month, none yet on this day.
\newcount\pcal@ypnm \newcount\pcal@ypnd \newif\ifpcal@ypfirstnote \newif\ifpcal@ypnewmonth \newif\ifpcal@ypnewday
\def\pcal@ypnotes{\pcal@ypfirstnotetrue\pcal@ypnm=\pcal@ypbase\relax\pcal@ypnotesm
    \ifpcal@ypfirstnote\else\par\fi}%
\def\pcal@ypnotesm{\ifnum\pcal@ypnm<\numexpr\pcal@ypbase+6\relax
    \pcal@ypnewmonthtrue\pcal@ypnd=1 \pcal@ypnotesd\advance\pcal@ypnm by 1 \expandafter\pcal@ypnotesm\fi}%
\def\pcal@ypnotesd{\ifnum\pcal@ypnd>\csname pcal@ypl\the\numexpr\pcal@ypnm-\pcal@ypbase+1\endcsname\relax\else
    \pcal@ypnewdaytrue
    \pcal@ypnoteof{pcal@ypn@\the\pcal@ypnm @\the\pcal@ypnd}%
    \pcal@ypnoteof{pcal@ypn@\pcal@ypyear @\the\pcal@ypnm @\the\pcal@ypnd}%
    \advance\pcal@ypnd by 1 \expandafter\pcal@ypnotesd\fi}%
\def\pcal@ypnoteof#1{\ifcsname#1\endcsname\csname#1\endcsname\fi}%
% One note: open the list, the month and the day as needed, then the text.
\def\pcal@n#1{\ifpcal@ypfirstnote\par\medskip\noindent\pcalplannernotesfont\fi
    \ifpcal@ypnewmonth
      \ifpcal@ypfirstnote\else\pcalplannernotemonthsep\fi
      \pcal@ypnoteargs\pcalplannernotemonth{}\pcal@ypnewmonthfalse
    \else\ifpcal@ypnewday\pcalplannernotesep\fi\fi
    \ifpcal@ypnewday\pcal@ypnoteargs\pcalplannernoteday{{\the\pcal@ypnd}}\pcal@ypnewdayfalse
    \else\pcalplannernotetextsep\fi
    \pcal@ypfirstnotefalse #1}%
% #1{year}{month}#2, with the year and month expanded.
\def\pcal@ypnoteargs#1#2{\edef\pcal@arg{{\pcal@ypyear}{\the\pcal@ypnm}}\expandafter#1\pcal@arg#2}%
% Julian day of the 1st of Jalali month #2 (may be 13) of year #1, into \pcal@ypj#3.
\def\pcal@ypfirst#1#2#3{{\pcal@temp=\numexpr#2\relax\pcal@temptwo=#1\relax
    \ifnum\pcal@temp>12 \advance\pcal@temp by -12 \advance\pcal@temptwo by 1 \fi
    \edef\pcal@arg{\the\pcal@temptwo-\the\pcal@temp-1}\expandafter\pcal@togregorian\expandafter{\pcal@arg}%
    \pgfcalendardatetojulian{\pcal@iso}{\pgfutil@tempcnta}%
    \expandafter\xdef\csname pcal@ypj#3\endcsname{\the\pgfutil@tempcnta}}}%
% Column #1 (1-6): weekday of its 1st (Saturday = 0) into \pcal@ypo#1, length into \pcal@ypl#1.
\def\pcal@ypmonth#1{{\pgfcalendarjuliantoweekday{\csname pcal@ypj#1\endcsname}{\pcal@temp}%
    \advance\pcal@temp by 2 \ifnum\pcal@temp>6 \advance\pcal@temp by -7 \fi
    \expandafter\xdef\csname pcal@ypo#1\endcsname{\the\pcal@temp}%
    \expandafter\xdef\csname pcal@ypl#1\endcsname{\the\numexpr\csname pcal@ypj\the\numexpr#1+1\endcsname
      -\csname pcal@ypj#1\endcsname\relax}}%
    \ifnum\numexpr\csname pcal@ypo#1\endcsname+\csname pcal@ypl#1\endcsname>\pcal@yprows
      \pcal@yprows=\numexpr\csname pcal@ypo#1\endcsname+\csname pcal@ypl#1\endcsname\relax\fi}%
% Expands to 1 if row #1 of column #2 holds a day, else 0.
\def\pcal@ypin#1#2{\ifnum\numexpr(#1)-\csname pcal@ypo#2\endcsname<0 0\else
    \ifnum\numexpr(#1)-\csname pcal@ypo#2\endcsname<\csname pcal@ypl#2\endcsname\space 1\else 0\fi\fi}%
\def\pcal@ypcell#1{&\ifnum\pcal@ypin\pcal@yprow{#1}=1 \noexpand\pcalplannercell{\pcal@ypyear}{\the\numexpr\pcal@ypbase+#1-1\relax}%
    {\the\numexpr\pcal@yprow-\csname pcal@ypo#1\endcsname+1\relax}{\the\pcal@ypwd}%
    \pcal@ypnotemark{\the\numexpr\pcal@ypbase+#1-1\relax}{\the\numexpr\pcal@yprow-\csname pcal@ypo#1\endcsname+1\relax}%
    \ifpcal@ypweeks\pcal@ypweekmark{\the\numexpr\pcal@ypbase+#1-1\relax}{\the\numexpr\pcal@yprow-\csname pcal@ypo#1\endcsname+1\relax}\fi
    \fi}%
% In the first cell of a week (a Saturday, or 1 Farvardin), the week number.
\def\pcal@ypweekmark#1#2{\ifnum\ifnum\pcal@ypwd=0 1\else\ifnum#1=1 \ifnum#2=1 1\else0\fi\else0\fi\fi=1
    \noexpand\pcal@ypweekcell{\pcal@ypweekno{#1}{#2}}\fi}%
% Rule under a cell that holds a day, or above one; full rules after Friday and the last row.
\def\pcal@ypcline#1{\ifnum\pcal@ypin\pcal@yprow{#1}\pcal@ypin{\pcal@yprow+1}{#1}>0
    \noexpand\cline{\the\numexpr#1+1\relax-\the\numexpr#1+1\relax}\fi}%
\def\pcal@yprule{\ifnum\pcal@yprow=\numexpr\pcal@yprows-1\relax\noexpand\hline
    \else\ifnum\pcal@ypwd=6 \noexpand\hline\noexpand\hline
    \else\noexpand\cline{1-1}\pcal@ypcline1\pcal@ypcline2\pcal@ypcline3\pcal@ypcline4\pcal@ypcline5\pcal@ypcline6\fi\fi}%
\def\pcal@yprowloop{\ifnum\pcal@yprow<\pcal@yprows
    \begingroup\edef\x{\endgroup\pcal@yptoks{\the\pcal@yptoks
      \noexpand\pcalplannerdaystrut\noexpand\pcalweekdayname{\the\pcal@ypwd}%
      \pcal@ypcell1\pcal@ypcell2\pcal@ypcell3\pcal@ypcell4\pcal@ypcell5\pcal@ypcell6\noexpand\\\pcal@yprule}}\x
    \advance\pcal@yprow by 1 \advance\pcal@ypwd by 1 \ifnum\pcal@ypwd=7 \pcal@ypwd=0 \fi
    \expandafter\pcal@yprowloop\fi}%
\def\pcal@ypwidest#1{\setbox\pgfutil@tempboxa\hbox{\pcalweekdayname{#1}}%
    \ifdim\wd\pgfutil@tempboxa>\dimen@\dimen@=\wd\pgfutil@tempboxa\fi}%
% One page: year #1, months #2 to #2+5.
\def\pcal@yphalf#1#2{\clearpage\thispagestyle{empty}\begingroup
    \pcal@ypfirst{#1}{#2}{1}\pcal@ypfirst{#1}{#2+1}{2}\pcal@ypfirst{#1}{#2+2}{3}%
    \pcal@ypfirst{#1}{#2+3}{4}\pcal@ypfirst{#1}{#2+4}{5}\pcal@ypfirst{#1}{#2+5}{6}\pcal@ypfirst{#1}{#2+6}{7}%
    \pcal@ypfirst{#1}{12}{e}\pcal@ypfirst{#1}{13}{f}\pcal@ypweeksetup{#1}%
    \edef\pcal@ypyear{\number#1}\edef\pcal@ypbase{\number\numexpr#2\relax}%
    \pcal@yprows=0 \pcal@ypmonth1\pcal@ypmonth2\pcal@ypmonth3\pcal@ypmonth4\pcal@ypmonth5\pcal@ypmonth6
    \pcal@yprow=0 \pcal@ypwd=0 \pcal@yptoks{}\pcal@yprowloop
    % the month columns share what the weekday column and the rules leave of the line
    \dimen@=0pt \pcal@ypwidest0\pcal@ypwidest1\pcal@ypwidest2\pcal@ypwidest3\pcal@ypwidest4\pcal@ypwidest5\pcal@ypwidest6
    \pcal@ypwidth=\dimexpr(\linewidth-\dimen@-14\tabcolsep-10\arrayrulewidth-\doublerulesep)/6\relax
    \noindent\hspace{1cm}{\LARGE #1}\hspace{8mm}%
    \ifnum\numexpr\pcal@ypjf-\pcal@ypje\relax=30 \pcalplannerleapyear\fi\par\medskip
    \noindent\begin{tabular}{|c||*{6}{p{\pcal@ypwidth}|}}\hline
      \multicolumn{1}{|c||}{\pcalplannermonthstrut}%
      \pcal@yphead{#2}\pcal@yphead{#2+1}\pcal@yphead{#2+2}\pcal@yphead{#2+3}\pcal@yphead{#2+4}\pcal@yphead{#2+5}\\
      \hline\hline
      \the\pcal@yptoks
    \end{tabular}\par\pcal@ypnotes\endgroup\clearpage}%
\def\pcal@yphead#1{&\multicolumn{1}{c|}{\pcalmonthname{\numexpr#1\relax}}}%
% \pcalplanner{year} with week numbers, \pcalplanner*{year} without.
\def\pcalplanner{\@ifstar{\pcal@planner\pcal@ypweeksfalse}{\pcal@planner\pcal@ypweekstrue}}%
\def\pcal@planner#1#2{\begingroup#1\pcal@yphalf{#2}{1}\pcal@yphalf{#2}{7}\endgroup}%
%
% Names before 0.71, kept so older documents still work; they call the new names, so they follow redefinitions.
\def\pcalendardatetojulian{\pcaldatetojulian}
\def\pcalendarjuliantodate{\pcaljuliantodate}
\def\pcalendaryearname{\pcalyearname}
\def\pcalendarmonthname{\pcalmonthname}
\def\pcalendargregorianmonthname{\pcalgregorianmonthname}
\def\pcalendarweekdayname{\pcalweekdayname}
\def\pcalendarweekdayshortname{\pcalweekdayshortname}
\endinput
%%
%% End of file `persian-calendar.sty'.
