0

I'm making a timetable where I draw rectangles with x-coordinates of the day and y-coordinates of the time. Centered inside these rectangles these is a node with some text. Because of several reasons, the fill color cannot be applied to the rectangle but has to be applied to the node itself. The problem is, that I cannot (or that I don't know how) apply a height to the node, so my fill color only spans around the text, but not around the entire time period of the entry.

How could I make the node as tall as the rectangle?


Here is some code of my project

\newcommand{\entry}[7]{
    \draw
        (#1, {time(#2)}) rectangle (#1+1, {time(#3)}) % {time(#2)} begin of the entry, {time(#3)} end of the entry
        node [
            rectangle split, 
            rectangle split parts=2, 
            pos = .5, 
            fill = #6, 
            text = #7, 
            text width = \entrytextwidth cm, 
            text centered
        ] 
        {\small{\textbf{#4}} \nodepart{second} \footnotesize{#5}};
}

Where time() is:

\pgfmathdeclarefunction{time}{1}{%
    \pgfmathparse{\firstH-(floor(#1)+(#1-floor(#1))/0.6)}%
 }

The time is passed as a float in the format hh.mm, \firstH is the first hour present on the timetable, in my case 8:00 am: 08.00.

The command is used as this: \calentry{1}{10.00}{10.45}{title}{location}{fillColor}{textColor} where 1 defines the weekday (Monday), 10.00 the starting time of the appointment (10:00 am) and 10.45 the ending time of the appointment (10:45 am).


Full code of the project

Please see my Github Gist. Feel free to suggest improvements.

3
  • 2
    regarding to your reputation you should know, that is simpler to help you, if you instead of code snippet provide complete small document -- mwe -- which we can copy and test in our computers. also give a link is not appropriate. it can be discontinue after while and members after than can not see, what is the problem.
    – Zarko
    Oct 9, 2017 at 15:59
  • minimum height=time(#3)-time(#2)? Untested though. Oct 9, 2017 at 16:14
  • minimum height doesn't apply to a multipart node. I propose (not tested): \draw (#1, {time(#2)}) rectangle (#1+1, {time(#3)}) node [pos=.5, minimum height=time(#3)-time(#2), text width= \entrytextwidth cm, text centered, fill=#6]{\phantom{\small{\textbf{#4}}}\\\phantom{\footnotesize{#5}}} node [ rectangle split, rectangle split parts=2, pos = .5, text = #7, text width = \entrytextwidth cm, text centered ] {\small{\textbf{#4}} \nodepart{second} \footnotesize{#5}}; }
    – Ignasi
    Oct 9, 2017 at 16:26

1 Answer 1

1

I would suggest the fit library:

\documentclass[border=2pt 2pt 2pt 2pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{fit} %% new library

%% some missing definitions
\def\entrytextwidth{2}
\def\firstH{0}

%% your new macro:
\newcommand{\entry}[7]{
    %% define the corners of the rectangle as coordinates:
    \path
        (#1, {time(#2)}) coordinate (A)
        (#1+1, {time(#3)}) coordinate (B)
        ;
    %% draw the rectangle:
    \draw (A) rectangle (B);
    %% draw the node:
    \node (fillcolor) [
            fit={(A) (B)}, %% fitting both coordinates
            inner sep = 0, %% do not take any more space than exactly fitting the height of the rectangle
            %rectangle split,
            %rectangle split parts=2,
            %pos = .5, %% not needed, as the node is not on the path anymore
            fill = #6,
            text width = \entrytextwidth cm,
        ] {};
    \node (title) at (fillcolor.center) [above]
        [text depth=0,text = #7,text centered]
        {\small{\textbf{#4}}};
    \node (location) at (fillcolor.center) [below]
        [text = #7,text centered]
        {\footnotesize{#5}};
    }

%% your function:
\pgfmathdeclarefunction{time}{1}{%
    \pgfmathparse{\firstH-(floor(#1)+(#1-floor(#1))/0.6)}%
}

\begin{document}
    \begin{tikzpicture}
        [x=5cm,y=2cm] %% otherwise it seems to be too small
        \entry{1}{10.00}{10.45}{title}{location}{green}{red}
    \end{tikzpicture}
\end{document}

However, this does not seem to work with the rectangle split, so two nodes separate nodes (title) and (location) are used for the text.

result

Note: since you didn't give an MWE, I just assumed some values.

2
  • This works pretty well, but the text is not exactly vertically centered. Do you know a workaround? anchor = mid doesn't do the job :/ PS: I updated the Github Gist with the current code, so that you can see the misalignment.
    – Sam
    Oct 9, 2017 at 21:14
  • I updated the answer to use two extra nodes for the title. With your github code this now looks better regarding to the centering.
    – Scz
    Oct 10, 2017 at 6:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .