1

How can I measure the height of a single word, but inside \newcommand which means the text may change every time.

Using the code in How to 'translate' a numerical value into a word with \newcommand? (with many thanks to Werner) and I modified to:

\documentclass{article}
\usepackage{color}

\newcommand{\Calendar}[3]{%
\noindent
\ifcase #1\or% 0
January\or % 1
February\or % 2
March\or % 3
April\or % 4
May\or % 5
June\or % 6
July\or % 7
August\or % 8
September\or % 9
October\or % 10
November\or % 11
December% 12
\fi
\\
\textcolor{blue}{\textit{#2%
\ifcase #2\or% 0
st\or % 1
nd\or % 2
rd\or % 3
th\or th\or th\or th\or th\or th\or th\or % 4, 5, 6, 7, 8, 9, 10,
th\or th\or th\or th\or th\or th\or th\or th\or th\or th\or % 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
st\or % 21
nd\or % 22
rd\or % 23
th\or th\or th\or th\or th\or th\or th\or % 24, 25, 26, 27, 28, 29, 30
st% 31
\fi}}
\\
\textcolor{red}{\textbf{#3}}%
\vspace{0.4em}
}

\begin{document}

\Calendar{2}{2}{1989}

\Calendar{1}{31}{1990}

\Calendar{3}{23}{1991}

\end{document}

It outputs the following:

enter image description here

I would like to measure the height of the first argument (#1, which is the month) so I can draw a box around the month only that exactly matches its height, but I don't know how to start.

6
  • 1
    Factor the macro into several separate macros (this macro can be split into two extra macros), then you can easily use \settowidth (look it up) on say \Month{#1}
    – daleif
    Aug 19, 2017 at 13:51
  • 1
    What will you do with this width?
    – Bernard
    Aug 19, 2017 at 13:57
  • 2
    If the width is to be queried, why is this tagged with height?
    – user31729
    Aug 19, 2017 at 21:36
  • 1
    In the title an the first sentence you are asking for the width in the last sentence and the tags you are asking for the height. That makes is somehow unclear. IMHO measuring the height does not make any sense here. So my answer is about the width. Nevertheless, you can get the height using \settoheight and \settodepth if you need it. Aug 20, 2017 at 10:38
  • Sorry, my question is unclear indeed. But I would like to make a box that matches the content to the pixel Aug 20, 2017 at 11:07

1 Answer 1

2

You don't need to measure the width. You can use either a tabular:

\documentclass{article}
\usepackage{color}

\newcommand{\Calendar}[3]{%
\noindent
\ifcase #1\or% 0
January\or % 1
February\or % 2
March\or % 3
April\or % 4
May\or % 5
June\or % 6
July\or % 7
August\or % 8
September\or % 9
October\or % 10
November\or % 11
December% 12
\fi
\\
\textcolor{blue}{\textit{#2%
\ifcase #2\or% 0
st\or % 1
nd\or % 2
rd\or % 3
th\or th\or th\or th\or th\or th\or th\or % 4, 5, 6, 7, 8, 9, 10,
th\or th\or th\or th\or th\or th\or th\or th\or th\or th\or % 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
st\or % 21
nd\or % 22
rd\or % 23
th\or th\or th\or th\or th\or th\or th\or % 24, 25, 26, 27, 28, 29, 30
st% 31
\fi}}
\\
\textcolor{red}{\textbf{#3}}%
\vspace{0.4em}
}

\begin{document}

\Calendar{2}{2}{1989}

\Calendar{1}{31}{1990}

\Calendar{3}{23}{1991}

\end{document}

tabular

or tabular inside \fbox:

\documentclass{article}
\usepackage{color}

\newcommand{\Calendar}[3]{%
  \noindent
  \fbox{\begin{tabular}{@{}l@{}}
\ifcase #1\or% 0
January\or % 1
February\or % 2
March\or % 3
April\or % 4
May\or % 5
June\or % 6
July\or % 7
August\or % 8
September\or % 9
October\or % 10
November\or % 11
December% 12
\fi
\\
\textcolor{blue}{\textit{#2%
\ifcase #2\or% 0
st\or % 1
nd\or % 2
rd\or % 3
th\or th\or th\or th\or th\or th\or th\or % 4, 5, 6, 7, 8, 9, 10,
th\or th\or th\or th\or th\or th\or th\or th\or th\or th\or % 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
st\or % 21
nd\or % 22
rd\or % 23
th\or th\or th\or th\or th\or th\or th\or % 24, 25, 26, 27, 28, 29, 30
st% 31
\fi}}
\\
\textcolor{red}{\textbf{#3}}\\
  \end{tabular}}%
}

\begin{document}

\Calendar{2}{2}{1989}

\Calendar{1}{31}{1990}

\Calendar{3}{23}{1991}

\end{document}

\fbox + tabular

or varwidth inside \fbox to make a box around it.

\documentclass{article}
\usepackage{color}
\usepackage{varwidth}

\newcommand{\Calendar}[3]{%
  \noindent
  \fbox{\begin{varwidth}{\linewidth}\raggedright
\ifcase #1\or% 0
January\or % 1
February\or % 2
March\or % 3
April\or % 4
May\or % 5
June\or % 6
July\or % 7
August\or % 8
September\or % 9
October\or % 10
November\or % 11
December% 12
\fi
\\
\textcolor{blue}{\textit{#2%
\ifcase #2\or% 0
st\or % 1
nd\or % 2
rd\or % 3
th\or th\or th\or th\or th\or th\or th\or % 4, 5, 6, 7, 8, 9, 10,
th\or th\or th\or th\or th\or th\or th\or th\or th\or th\or % 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
st\or % 21
nd\or % 22
rd\or % 23
th\or th\or th\or th\or th\or th\or th\or % 24, 25, 26, 27, 28, 29, 30
st% 31
\fi}}
\\
\textcolor{red}{\textbf{#3}}\\
  \end{varwidth}}%
}

\begin{document}

\Calendar{2}{2}{1989}

\Calendar{1}{31}{1990}

\Calendar{3}{23}{1991}

\end{document}

varwidth + \fbox

This does also work for very short month like May, where the yer could be wider than the name of the month. Nevertheless, if you like to measure, use \settowidth or \widthof:

\documentclass{article}
\usepackage{color}
\usepackage{calc}% for \widthof


\newcommand*{\Month}[1]{%
  \ifcase #1\or% 0
  January\or % 1
  February\or % 2
  March\or % 3
  April\or % 4
  May\or % 5
  June\or % 6
  July\or % 7
  August\or % 8
  September\or % 9
  October\or % 10
  November\or % 11
  December% 12
  \fi
}
\newcommand{\Calendar}[3]{%
  \noindent
  \fbox{\begin{minipage}{\widthof{\Month{#1}}}\raggedright
    \Month{#1}\\
\textcolor{blue}{\textit{#2%
\ifcase #2\or% 0
st\or % 1
nd\or % 2
rd\or % 3
th\or th\or th\or th\or th\or th\or th\or % 4, 5, 6, 7, 8, 9, 10,
th\or th\or th\or th\or th\or th\or th\or th\or th\or th\or % 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
st\or % 21
nd\or % 22
rd\or % 23
th\or th\or th\or th\or th\or th\or th\or % 24, 25, 26, 27, 28, 29, 30
st% 31
\fi}}
\\
\textcolor{red}{\textbf{#3}}\\
  \end{minipage}}%
}

\begin{document}

\Calendar{2}{2}{1989}

\Calendar{1}{31}{1990}

\Calendar{5}{23}{1991}% Problem!!!

\end{document}

measure with May problem

BTW: For more nice boxes see tcolorbox.

You must log in to answer this question.

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