我如何测量高度一个单词,但是里面 \newcommand
这意味着文本每次都可能会改变。
使用代码如何使用 \newcommand 将数值“翻译”为单词?(非常感谢 Werner)我修改为:
\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}
它输出以下内容:
我想测量高度第一个参数(#1,即月份),这样我就可以在月份周围画一个框仅有的那确切地与它的高度相匹配,但我不知道如何开始。
答案1
您不需要测量宽度。您可以使用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
内部\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}
或varwidth
在里面\fbox
制作一个盒子围绕它。
\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}
这也适用于像 5 月这样非常短的月份,其中月份可能比月份名称更宽。不过,如果您喜欢测量,请使用\settowidth
或\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}
顺便说一句:欲了解更多精美的盒子,请参阅tcolorbox
。