1

I am trying to branch of font size and the following is not working as I would expect:

\def\tinyfont{tiny}
\def\scriptfont{scriptsize}
\newcommand{\setfontsize}[1]{%
    \def\currentsize{#1}%
    \ifx\tinyfont\currentsize%
        \tiny%
    \fi%
    \ifx\scriptfont\currentsize%
        \scriptsize%
    \fi%
}

When used like this, it will work:

{\setfontsize{tiny}Hello!}

but when used like this, it will NOT work:

\newcommand{\myfontsize}{tiny}
{\setfontsize{tiny}Hello!}

QUESTIONS:

  1. How to make it work?
  2. Why is not working?
  3. What would be the "right" way to write a function that converts a font size as a text ("tiny", "scriptsize", ..., "huge", "Huge") to the right font size command?

EDIT: Reacting to the comments, for 1. and 2., how to make it work keeping the if comparisons, for example to handle the case where the text would not be the name of the command as in: \setfontstyle{bf} => \bfseries?

7
  • As to question 3, \newcommand\setfontsize[1]{\csname#1\endcsname} And then invoke \setfontsize{tiny}. Commented Nov 29, 2022 at 1:58
  • In your example of what doesn't work, I presume you meant \newcommand{\myfontsize}{tiny} {\setfontsize{\myfontsize}Hello!}. If you use my code from my above comment, it should answer question #1. Commented Nov 29, 2022 at 2:01
  • And how would I do it if the name to be provided was not the same as the name of the command. For example \setfontstyle{bf} => \bfseries?
    – Vincent
    Commented Nov 29, 2022 at 2:41
  • I would use separate commands like \newcommand\setfontseries[1]{\csname#1series\endcsname} and \newcommand\setfontshape[1]{\csname#1shape\endcsname}, with invocations like \setfontseries{bf} and \setfontshape{it}. Commented Nov 29, 2022 at 3:41
  • \edef\currentsize{#1}% Commented Nov 29, 2022 at 9:30

1 Answer 1

6

I suppose that you want to say: when I use

\newcommand{\tinyfont}{tiny}  % instead \def\tinyfont{tiny}
{\setfontsize{tiny}Hello!}

then it doesn't work.

The reason is that LaTeX's \newcommand defines the macro \tinyfont with \long prefix. On the other hand your code includes \def\currsize{#1} so the macro \currsize isn't defined with \long prefix. Two identical macros, first with and second without \long are not decided as the same by \ifx. You can use \newcommand*{\tinyfont}{tiny} or you can declare \long\def\currsize{#1} in your code or you can avoid to use \newcommand or you can avoid to use LaTeX.

You must log in to answer this question.

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