2

I've this code that suppose to draw a schedule/schema/timetable

\documentclass[a4paper]{article}
\usepackage[top=5mm, bottom=5mm, left=5mm, right=5mm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{graphics}
\usetikzlibrary{shapes.multipart}

%Options for timetable contents
\def\firsthour{8}
\def\lasthour{17}

%Options for timetable drawing
\def\daywidth{3.4cm}   %approx \textwidth / 6
\def\hourheight{2.2cm} %approx \textheight / (\lasthour - \firsthour + 1)

\begin{document}

\begin{tikzpicture}[
    x=\daywidth, y=-\hourheight,
    block/.style={
        draw, text width=\daywidth, minimum height=\hourheight, inner sep=0pt, align=flush center
    },
    hour/.style ={block, fill=yellow!40, font=\bfseries\Large,
        text width=0.6*\daywidth, xshift=0.2*\daywidth},
    day/.style  ={block, fill=yellow!40, font=\bfseries\Large,
        minimum height=0.5*\hourheight, yshift=-0.25*\hourheight},
    week/.style ={block, fill=yellow!40, font=\bfseries\Large,
        minimum height=0.5*\hourheight, yshift=-0.25*\hourheight,
        text width=0.6*\daywidth, xshift=0.2*\daywidth},
    name/.style ={font=\normalsize},
    hours/.style={minimum height=#1*\hourheight}
]\tikzset{ AtHome/.style={block, fill=red!20, draw=red!50!black, thick} }
\tikzset{ AtJobCoach/.style={block, fill=green!20, draw=green!50!black, thick} }
\tikzset{ PersonalCoaching/.style={block, fill=blue!20, draw=blue!50!black, thick} }
\tikzset{ SYV/.style={block, fill=cyan!20, draw=orange!50!black, thick} }
\tikzset{ LUNCH/.style={block, fill=orange!20, draw=purple!50!black, thick} }


\draw[help lines, xshift=0.5*\daywidth, yshift=0.5*\hourheight]
    (0, \firsthour) grid [xstep=\daywidth, ystep=\hourheight] (5, \lasthour);

\pgfmathtruncatemacro\secondhour{\firsthour + 1}
\foreach \end[remember=\end as \start (initially \firsthour)] in {\secondhour, ..., \lasthour} {
    \node[hour] at (0, \start) {\start--\end};
}

\node[week] at (0, \firsthour-1) {v.40};
\node[day] at (1, \firsthour-1) {{\small 30/09} Måndag};
\node[day] at (2, \firsthour-1) {{\small 01/10} Tisdag};
\node[day] at (3, \firsthour-1) {{\small 02/10} Onsdag};
\node[day] at (4, \firsthour-1) {{\small 03/10} Torsdag};
\node[day] at (5, \firsthour-1) {{\small 04/10} Fredag};

\node[AtHome, hours=4] at ( 2 , 8 ) {
    \nodepart[name]{one} \strut TEXT 4 hours
};
\node[LUNCH, hours=1] at ( 2 , 12 ) {    
    \nodepart[name]{one} \strut LUNCH
};
\node[AtJobCoach, hours=3] at ( 2 , 14 ) {
    \nodepart[name]{one} \strut TEXT 3 hours
};
\node[AtHome, hours=1] at ( 2 , 16 ) {
    \nodepart[name]{one} \strut TEXT 1 hour
};
\end{tikzpicture}
\end{document}

The row of daynames (Måndag, Tisdag, Onsdag, etc.) all works fine and are in level, but the node "TEXT 4 hours" starts way above the day names making the whole timetable to look strange. Of course I can change the 8 in (2, 8) to make it level correctly, but that shouldn't be needed and I can't figure out a system for how much I should correct the 8 to be able to get a good schedule.

What's wrong with the above code?

2

Basically the nodes are centered around the coordinate you specify. If you add anchor=north,yshift=0.5*\hourheight to the block style, and perhaps modify the other yshifts in the styles (I lost track a bit, but I can highlight the changes later, if you like).

With the code below you specify the start hour of each block, hence the change from 14 to 13 for the AtJobCoach one. I haven't tested anything else than what was already in the code, so if anything fails miserably, let me know.

\documentclass[a4paper]{article}
\usepackage[top=5mm, bottom=5mm, left=5mm, right=5mm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{tikz} % loads graphicx
\usetikzlibrary{shapes.multipart}

%Options for timetable contents
\def\firsthour{8}
\def\lasthour{17}

%Options for timetable drawing
\def\daywidth{3.4cm}   %approx \textwidth / 6
\def\hourheight{2.2cm} %approx \textheight / (\lasthour - \firsthour + 1)

\begin{document}

\begin{tikzpicture}[
    x=\daywidth, y=-\hourheight,
    block/.style={
        draw, text width=\daywidth, minimum height=\hourheight, inner sep=0pt, align=flush center, anchor=north, yshift=0.5*\hourheight
    },
    hour/.style ={block, fill=yellow!40, font=\bfseries\Large,
        text width=0.6*\daywidth, xshift=0.2*\daywidth},
    day/.style  ={block, fill=yellow!40, font=\bfseries\Large,
        minimum height=0.5*\hourheight, yshift=-0.5*\hourheight,},
    week/.style ={block, fill=yellow!40, font=\bfseries\Large,
        minimum height=0.5*\hourheight, yshift=-0.5*\hourheight,
        text width=0.6*\daywidth, xshift=0.2*\daywidth},
    name/.style ={font=\normalsize},
    hours/.style={minimum height=#1*\hourheight},
    AtHome/.style={block, fill=red!20, draw=red!50!black, thick},
    AtJobCoach/.style={block, fill=green!20, draw=green!50!black, thick},
    PersonalCoaching/.style={block, fill=blue!20, draw=blue!50!black, thick},
    SYV/.style={block, fill=cyan!20, draw=orange!50!black, thick},
    LUNCH/.style={block, fill=orange!20, draw=purple!50!black, thick}
]


\draw[help lines, xshift=0.5*\daywidth, yshift=0.5*\hourheight]
    (0, \firsthour) grid [xstep=\daywidth, ystep=\hourheight] (5, \lasthour);

\pgfmathtruncatemacro\secondhour{\firsthour + 1}
\foreach \ending[remember=\ending as \start (initially \firsthour)] in {\secondhour, ..., \lasthour} {
    \node[hour] at (0, \start) {\start--\ending};
}

\node[week] at (0, \firsthour-1) {v.40};
\node[day] at (1, \firsthour-1) {{\small 30/09} Måndag};
\node[day] at (2, \firsthour-1) {{\small 01/10} Tysdag};
\node[day] at (3, \firsthour-1) {{\small 02/10} Onsdag};
\node[day] at (4, \firsthour-1) {{\small 03/10} Torsdag};
\node[day] at (5, \firsthour-1) {{\small 04/10} Fredag};

\node[AtHome, hours=4] at ( 2 , 8 ) {
    \nodepart[name]{one} \strut TEXT 4 hours
};
\node[LUNCH, hours=1] at ( 2 , 12 ) {    
    \nodepart[name]{one} \strut LUNCH
};
\node[AtJobCoach, hours=3] at ( 2 , 13 ) {
    \nodepart[name]{one} \strut TEXT 3 hours
};
\node[AtHome, hours=1] at ( 2 , 16 ) {
    \nodepart[name]{one} \strut TEXT 1 hour
};

\end{tikzpicture}
\end{document}

enter image description here

  • Oh, I changed the loop variable from \end to \ending. Even though it obviously doesn't break \end, seems better to stay away from such a basic macro, just as a matter of principle. – Torbjørn T. Oct 18 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.