aboutsummaryrefslogtreecommitdiffstats
path: root/weekschedule.cls
blob: 75925b3498f72701e9d8236913f3f1815457ca77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
% weekschedule.cls - A LaTeX class for creating weekly schedule calendars
%
% Copyright (c) 2026, Douglas B. Rumbaugh
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% 1. Redistributions of source code must retain the above copyright notice,
%    this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright notice,
%    this list of conditions and the following disclaimer in the documentation
%    and/or other materials provided with the distribution.
%
% 3. Neither the name of the copyright holder nor the names of its
%    contributors may be used to endorse or promote products derived from this
%    software without specific prior written permission.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
%
% Usage:
%   \documentclass{weekschedule}
%   \scheduletitle{Weekly Schedule - Spring 2026}
%   \scheduleauthor{Dr. Smith}
%   \timefrom{8:00}
%   \timeto{18:00}
%   \twelvehourtime  % or \twentyfourhourtime
%   \eventclass{Courses}{173,216,230}  % RGB values
%   \event{Courses}{MATH 101}{Monday,Wednesday,Friday}{9:00}{10:00}
%   \begin{document}
%   \printschedule
%   \end{document}

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{weekschedule}[2026/01/11 Weekly Schedule Class]

% Load base class
\LoadClass[landscape,11pt]{article}

% Required packages
\RequirePackage[margin=0.5in]{geometry}
\RequirePackage{tikz}
\RequirePackage{xcolor}
\RequirePackage{pgfmath}

\pagestyle{empty}

% ============================================
% Configuration variables with defaults
% ============================================
\newcommand{\@scheduletitle}{Weekly Schedule}
\newcommand{\@scheduleauthor}{}
\newcommand{\@starthour}{8}
\newcommand{\@endhour}{18}
\newif\if@usetwentyfourhour
\@usetwentyfourhourfalse

% Layout parameters
\newcommand{\@daywidth}{4.5}
\newcommand{\@hourheight}{1.5}
\newcommand{\@padding}{0.08}

% Time label configuration
\newcommand{\@timelabelxpos}{-0.1}      % X position of time labels
\newcommand{\@timelabelreserved}{1.5}   % Space reserved for time labels

% Week configuration
\newcommand{\@weekdaylist}{Monday,Tuesday,Wednesday,Thursday,Friday}
\newcommand{\@daycount}{5}

% Minute line configuration
\newcommand{\@minutelineinterval}{0}  % 0=off, 30=half-hour, 15=quarter-hour

% Spacing constants
\newcommand{\@titlespace}{0.05cm}
\newcommand{\@legendspace}{0.2cm}
\newcommand{\@dayheaderoffset}{0.2}     % Vertical offset for day headers

% ============================================
% User-facing configuration commands
% ============================================
\newcommand{\scheduletitle}[1]{\renewcommand{\@scheduletitle}{#1}}
\newcommand{\scheduleauthor}[1]{\renewcommand{\@scheduleauthor}{#1}}

% Parse time string like "8:00" or "13:30" and store hour (integer part only)
\newcommand{\timefrom}[1]{%
    \@parsetime{#1}%
    \pgfmathtruncatemacro{\@starthour}{\@parsedtime}%
}

\newcommand{\timeto}[1]{%
    \@parsetime{#1}%
    \pgfmathtruncatemacro{\@endhour}{\@parsedtime}%
}

% Time format toggles
\newcommand{\twentyfourhourtime}{\@usetwentyfourhourtrue}
\newcommand{\twelvehourtime}{\@usetwentyfourhourfalse}

% Week configuration toggles
\newcommand{\weekworkweek}{%
    \renewcommand{\@weekdaylist}{Monday,Tuesday,Wednesday,Thursday,Friday}%
    \renewcommand{\@daycount}{5}%
}

\newcommand{\weekfullsunday}{%
    \renewcommand{\@weekdaylist}{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday}%
    \renewcommand{\@daycount}{7}%
}

\newcommand{\weekfullmonday}{%
    \renewcommand{\@weekdaylist}{Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday}%
    \renewcommand{\@daycount}{7}%
}

% Layout customization
\newcommand{\setpadding}[1]{\renewcommand{\@padding}{#1}}

% Minute line configuration
\newcommand{\minutelinesoff}{\renewcommand{\@minutelineinterval}{0}}
\newcommand{\minutelinesthirty}{\renewcommand{\@minutelineinterval}{30}}
\newcommand{\minutelinesfifteen}{\renewcommand{\@minutelineinterval}{15}}

% ============================================
% Internal time parsing
% ============================================
% Parse time to decimal and round to nearest 15-minute interval
% (e.g., "9:04" -> 9.0, "9:14" -> 9.25)
\def\@parsetime#1{\@@parsetime#1\@nil}
\def\@@parsetime#1:#2\@nil{%
    % Convert to total minutes
    \pgfmathparse{#1 * 60 + #2}%
    \pgfmathtruncatemacro{\@totalminutes}{\pgfmathresult}%
    % Round to nearest 15 minutes
    \pgfmathparse{round(\@totalminutes/15)*15}%
    \pgfmathtruncatemacro{\@roundedminutes}{\pgfmathresult}%
    % Convert back to decimal hours
    \pgfmathparse{\@roundedminutes/60}%
    \edef\@parsedtime{\pgfmathresult}%
}

% ============================================
% Event class definitions (colors)
% ============================================
\newcounter{@numclasses}
\newcounter{@daypos}

% Define an event class with RGB color
% Usage: \eventclass{ClassName}{R,G,B}
\newcommand{\eventclass}[2]{%
    \definecolor{class@#1}{RGB}{#2}%
    \stepcounter{@numclasses}%
    \expandafter\def\csname @classname\the@numclasses\endcsname{#1}%
}

% ============================================
% Event storage
% ============================================
\newcounter{@numevents}
\setcounter{@numevents}{0}

% Store an event
% Usage: \event{ClassName}{Event Name}{Days}{Start}{End}
% Days can be: Monday, Tuesday, Wednesday, Thursday, Friday (comma-separated)
\newcommand{\event}[5]{%
    \stepcounter{@numevents}%
    \expandafter\def\csname @eventclass\the@numevents\endcsname{#1}%
    \expandafter\def\csname @eventname\the@numevents\endcsname{#2}%
    \expandafter\def\csname @eventdays\the@numevents\endcsname{#3}%
    \expandafter\def\csname @eventstart\the@numevents\endcsname{#4}%
    \expandafter\def\csname @eventend\the@numevents\endcsname{#5}%
}

% ============================================
% Main schedule rendering
% ============================================
\newcommand{\printschedule}{%
    % Title and author
    \begin{center}
    \LARGE\textbf{\@scheduletitle}%
    \ifx\@scheduleauthor\empty\else\\[0.2cm]\Large\@scheduleauthor\fi%
    \end{center}
    \vspace{\@titlespace}
    
    \centering
    \begin{tikzpicture}[x=1cm, y=1cm]
        % Calculate dynamic day width based on number of days
        % Reserve space for time labels, divide remaining width by day count
        \pgfmathsetmacro{\availablewidth}{(\textwidth/1cm) - \@timelabelreserved}
        \pgfmathsetmacro{\daywidth}{\availablewidth / \@daycount}

        % Store other parameters
        \pgfmathsetmacro{\hourheight}{\@hourheight}
        \pgfmathsetmacro{\padding}{\@padding}
        \pgfmathtruncatemacro{\starthour}{\@starthour}
        \pgfmathtruncatemacro{\endhour}{\@endhour}
        
        % Calculate grid width (used for drawing)
        \pgfmathsetmacro{\gridwidth}{\@daycount*\daywidth}
        
        % Calculate shift to center the grid properly
        % \centering centers the bounding box, which includes time labels extending left.
        % To align grid center with bounding box center (which is at page center),
        % shift right by half the reserved time label space.
        \pgfmathsetmacro{\xshift}{\@timelabelreserved/2}
        
        % Shift everything to align grid center with page center
        \begin{scope}[xshift=\xshift cm]

        % Draw column headers (days) - iterate through configured day list
        \setcounter{@daypos}{0}
        \@for\@currentday:=\@weekdaylist\do{%
            \node[anchor=south, font=\large\bfseries] at (\the@daypos*\daywidth+\daywidth/2, -\starthour*\hourheight+\@dayheaderoffset) {\@currentday};
            \stepcounter{@daypos}%
        }

        % Draw row headers (times)
        \foreach \hour in {\starthour,...,\endhour} {
            \if@usetwentyfourhour
                \node[anchor=east, font=\small] at (\@timelabelxpos, -\hour*\hourheight) {\hour:00};
            \else
                \pgfmathtruncatemacro{\displayhour}{mod(\hour-1,12)+1}
                \ifnum\hour<12
                    \def\@ampm{AM}
                \else
                    \def\@ampm{PM}
                \fi
                \node[anchor=east, font=\small] at (\@timelabelxpos, -\hour*\hourheight) {\displayhour:00 \@ampm};
            \fi
        }

        % Draw vertical lines (day separators) - dynamically based on week type
        \foreach \day in {0,...,\@daycount} {
            \draw[gray] (\day*\daywidth, -\starthour*\hourheight) -- (\day*\daywidth, -\endhour*\hourheight);
        }

        % Draw horizontal lines (hour separators)
        \foreach \hour in {\starthour,...,\endhour} {
            \draw[gray] (0, -\hour*\hourheight) -- (\gridwidth, -\hour*\hourheight);
        }

        % Draw minute lines (dashed) based on configuration
        \pgfmathtruncatemacro{\lasthour}{\endhour-1}
        \pgfmathtruncatemacro{\minuteinterval}{\@minutelineinterval}
        \ifnum\minuteinterval=30
            % Draw half-hour lines
            \foreach \hour in {\starthour,...,\lasthour} {
                \draw[gray, dashed, line width=0.25pt] (0, -\hour*\hourheight-0.5*\hourheight) -- (\gridwidth, -\hour*\hourheight-0.5*\hourheight);
            }
        \fi
        \ifnum\minuteinterval=15
            % Draw quarter-hour lines
            \foreach \hour in {\starthour,...,\lasthour} {
                \draw[gray, dashed, line width=0.25pt] (0, -\hour*\hourheight-0.25*\hourheight) -- (\gridwidth, -\hour*\hourheight-0.25*\hourheight);
                \draw[gray, dashed, line width=0.25pt] (0, -\hour*\hourheight-0.5*\hourheight) -- (\gridwidth, -\hour*\hourheight-0.5*\hourheight);
                \draw[gray, dashed, line width=0.25pt] (0, -\hour*\hourheight-0.75*\hourheight) -- (\gridwidth, -\hour*\hourheight-0.75*\hourheight);
            }
        \fi

        % Draw all events
        \foreach \evnum in {1,...,\the@numevents} {
            \@drawevent{\evnum}
        }
        \end{scope}
    \end{tikzpicture}
    \par

    \vspace{\@legendspace}

    % Legend
    \begin{center}
    \begin{tikzpicture}
        \pgfmathsetmacro{\@legendspacing}{3.5}      % Horizontal spacing between legend items
        \pgfmathsetmacro{\@legendboxwidth}{0.4}     % Width of legend color box
        \pgfmathsetmacro{\@legendboxheight}{0.3}    % Height of legend color box
        \pgfmathsetmacro{\@legendtextoffset}{0.5}   % Offset from box to text
        \pgfmathsetmacro{\@legendycenter}{0.15}      % Vertical center of legend items
        \foreach \clnum in {1,...,\the@numclasses} {
            \pgfmathsetmacro{\xpos}{(\clnum-1)*\@legendspacing}
            \edef\@clname{\csname @classname\clnum\endcsname}
            \fill[class@\@clname] (\xpos,0) rectangle (\xpos+\@legendboxwidth,\@legendboxheight);
            \node[anchor=west] at (\xpos+\@legendtextoffset,\@legendycenter) {\@clname};
        }
    \end{tikzpicture}
    \end{center}
}

% ============================================
% Event drawing helper
% ============================================
\newcommand{\@drawevent}[1]{%
    \edef\@evclass{\csname @eventclass#1\endcsname}%
    \edef\@evname{\csname @eventname#1\endcsname}%
    \edef\@evdays{\csname @eventdays#1\endcsname}%
    \edef\@evstart{\csname @eventstart#1\endcsname}%
    \edef\@evend{\csname @eventend#1\endcsname}%
    % Parse times
    \expandafter\@parsetime\expandafter{\@evstart}%
    \let\@startdec\@parsedtime
    \expandafter\@parsetime\expandafter{\@evend}%
    \let\@enddec\@parsedtime
    % Draw for each day
    \@drawforeachday{\@evclass}{\@evname}{\@evdays}{\@startdec}{\@enddec}%
}

\newcounter{@evdaypos}

\newcommand{\@drawforeachday}[5]{%
    % #1=class, #2=name, #3=event days, #4=start, #5=end
    % Iterate through the week's day list and draw event if it's on that day
    \setcounter{@evdaypos}{0}
    \@for\@weekday:=\@weekdaylist\do{%
        \@checkandrawday{#1}{#2}{#3}{#4}{#5}{\@weekday}{\the@evdaypos}%
        \stepcounter{@evdaypos}%
    }%
}

\newcommand{\@checkandrawday}[7]{%
    % #1=class, #2=name, #3=event days string, #4=start, #5=end, #6=dayname, #7=daypos
    % Check if this day is in the event's day list
    \@checkdayinlist{#3}{#6}%
    \if@daymatched
        \@draweventbox{#1}{#2}{#7}{#4}{#5}%
    \fi
}

\newif\if@daymatched

\newcommand{\@checkdayinlist}[2]{%
    % #1 = comma-separated day list, #2 = day to check
    \@daymatchedfalse
    \edef\@daytocheck{#2}%
    \@for\@testday:=#1\do{%
        \edef\@trimmedday{\@testday}%
        \ifnum\pdfstrcmp{\@trimmedday}{\@daytocheck}=0
            \@daymatchedtrue
        \fi
    }%
}

\newcommand{\@draweventbox}[5]{%
    % #1 = class, #2 = name, #3 = day number, #4 = start decimal, #5 = end decimal
    % Note: daywidth, hourheight, padding are already set in parent scope
    % Clamp start and end times to calendar bounds
    \pgfmathsetmacro{\startdec}{max(#4, \@starthour)}
    \pgfmathsetmacro{\enddec}{min(#5, \@endhour)}
    \pgfmathsetmacro{\xstart}{#3*\daywidth+\padding}
    \pgfmathsetmacro{\xend}{#3*\daywidth+\daywidth-\padding}
    \pgfmathsetmacro{\ystart}{-\startdec*\hourheight-\padding}
    \pgfmathsetmacro{\yend}{-\enddec*\hourheight+\padding}
    \pgfmathsetmacro{\ymid}{(\ystart+\yend)/2}
    \pgfmathsetmacro{\xcenter}{#3*\daywidth+\daywidth/2}
    % Draw the box
    \fill[class@#1, rounded corners] (\xstart, \ystart) rectangle (\xend, \yend);
    % Draw the label
    \node[font=\small, align=center] at (\xcenter, \ymid) {#2};
}

\endinput