5

I am trying to identify whether the current font size is small. So here is a MWE of what I am trying to do

\documentclass{article}
\usepackage{ifthen}
\begin{document}

\makeatletter
\ifthenelse{\f@size = 9}{the size is 9}{the size is not 9}
\makeatother

\makeatletter
\small
\ifthenelse{\f@size = 9}{the size is 9}{the size is not 9}
\makeatother

\makeatletter
\ifthenelse{\f@size = {\small \f@size} }{the size is small}{the size is not small}
\makeatother

\end{document}

Well my desired output would be something like

output of the font size

but somehow {\small \f@size} is not "expanded" as a number, which actually is. Thanks for help. :)

4 Answers4

6

I'd just query \@currsize.

\documentclass{article}
\makeatletter
\newcommand\queryfont[3]{%
  \ifx\@currsize#1
    #2%
  \else
    #3%
  \fi
}
\makeatother
\begin{document}
\queryfont\small{This is small}{This is not small}

\small
\queryfont\small{This is small}{This is not small}
\end{document}

enter image description here

Henri Menke
  • 109,596
4

{\small \f@size} is not a number. You should better store the f@size-value first:

\documentclass{article}
\usepackage{ifthen}
\begin{document}

\makeatletter
\ifthenelse{\f@size=9}{the size is 9}{the size is not 9}
\makeatother

\makeatletter
\small
\ifthenelse{\f@size=9}{the size is 9}{the size is not 9}
\makeatother

\makeatletter
{\small \xdef\smallfsize{\f@size}}
\ifthenelse{\f@size=\smallfsize}{the size is small}{the size is not small}
\makeatother

\end{document}
Ulrike Fischer
  • 327,261
  • That's it. Thanks a lot! I knew the answer had to do with some kind of expansion at the definition. By the way @Christian Hupfer. You answered almost simultaneously. Sorry I can only choose one answer :) – loved.by.Jesus Feb 05 '16 at 16:51
  • 1
    @loved.by.Jesus: Actually, I was faster ;-) –  Feb 05 '16 at 16:53
4

Is this what you have in mind? The \definesizecommand macro is able to define a command that behaves differently in the various sizes. This command can also have arguments, as shown by \baz.

\documentclass{article}
\usepackage{relsize}
\usepackage{xparse}

\ExplSyntaxOn
% relsize builds a list \rs@fontsizes of the form
%   \rs@size\normalfont{10.0pt}\rs@size\small{9.0pt}...
% and we can use it for building a different list

\prop_new:N \g_lbj_size_to_name_prop
\prop_new:N \g_lbj_name_to_size_prop
\group_begin:
\cs_set:cpn { rs@size } #1 #2
 {
  \prop_gput:Nnx \g_lbj_size_to_name_prop { #2 } { \cs_to_str:N #1 }
 }
\use:c { rs@fontsizes }
% this is not used, but could become handy
\cs_set:cpn { rs@size } #1 #2
 {
  \prop_gput:Nnx \g_lbj_name_to_size_prop { \cs_to_str:N #1 } { #2 }
 }
\use:c { rs@fontsizes }
\group_end:

% just print the current size name
\NewDocumentCommand{\currentsizename}{}
 {
  \prop_item:Nf \g_lbj_size_to_name_prop { \dim_eval:n { \use:c {f@size} pt } }
 }
\cs_generate_variant:Nn \prop_item:Nn { Nf }

\NewDocumentCommand{\definesizecommand}{mmmO{}}
 {% #1 is the command to define,
  % #2 is the argument specifier,
  % #3 is the list of actions,
  % #4 is the optional action to do in uncovered cases
  \NewDocumentCommand{#1}{#2}
   {
    \str_case:xnF
     { \prop_item:Nf \g_lbj_size_to_name_prop { \dim_eval:n { \use:c { f@size } pt } } }
     { #3 }
     { #4 }
   }
 }
\cs_generate_variant:Nn \str_case:nnF { x }
\ExplSyntaxOff

\definesizecommand{\foo}{}{
  {normalsize}{normalfoo}
  {small}{smallfoo}
  {tiny}{tinyfoo}
}[extrafoo]

\definesizecommand{\baz}{m}{
  {normalsize}{---#1---}
  {small}{!#1!}
  {tiny}{?#1?}
}[(#1)]

\begin{document}

Here we're in \currentsizename: \foo\ and \baz{x}

\small
Here we're in \currentsizename: \foo\ and \baz{y}

\tiny
Here we're in \currentsizename: \foo\ and \baz{z}

\Large
Here we're in \currentsizename: \foo\ and \baz{A}

\end{document}

enter image description here

A different version without loading relsize, but exploiting \@currsize:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\cs_new:Nn \lbj_curr_size:
 {
  \__lbj_curr_size:c { @currsize }
 }

\cs_new:Nn \__lbj_curr_size:N
 {
  \exp_after:wN \__lbj_curr_size_aux:NNw #1 \q_stop
 }
\cs_generate_variant:Nn \__lbj_curr_size:N { c }

\cs_new:Npn \__lbj_curr_size_aux:NNw #1 #2 #3 \q_stop
 {
  \tl_if_blank:nTF { #3 }
   {
    \__lbj_curr_size:N #2
   }
   {
    \cs_to_str:N #2
   }
 }

% just print the current size name
\NewDocumentCommand{\currentsizename}{}
 {
  \lbj_curr_size:
 }

\NewDocumentCommand{\definesizecommand}{mmmO{}}
 {% #1 is the command to define,
  % #2 is the argument specifier,
  % #3 is the list of actions,
  % #4 is the optional action to do in uncovered cases
  \NewDocumentCommand{#1}{#2}
   {
    \str_case:fnF
     { \lbj_curr_size: }
     { #3 }
     { #4 }
   }
 }
\cs_generate_variant:Nn \str_case:nnF { f }
\ExplSyntaxOff

\definesizecommand{\foo}{}{
  {normalsize}{normalfoo}
  {small}{smallfoo}
  {tiny}{tinyfoo}
}[extrafoo]

\definesizecommand{\baz}{m}{
  {normalsize}{---#1---}
  {small}{!#1!}
  {tiny}{?#1?}
}[(#1)]

\begin{document}

Here we're in \currentsizename: \foo\ and \baz{x}

\small
Here we're in \currentsizename: \foo\ and \baz{y}

\tiny
Here we're in \currentsizename: \foo\ and \baz{z}

\Large
Here we're in \currentsizename: \foo\ and \baz{A}

\end{document}
egreg
  • 1,121,712
  • just spectacular (!) Actually, that is what I was trying to implement. Therefore you broke the "tie" between Ulrike and Christian with this complete solution. Wow! The other solutions are more didactical, easy to understand but yours is top. Thanks for taking so much time. :) – loved.by.Jesus Feb 05 '16 at 17:24
3

I've stored the font size \f@size of \small first (before \begin{document} and use \ifthenelse{...} to compare this with the current size.

\documentclass{article}

\usepackage{xifthen}
\makeatletter
{\small\xdef\smallfontsize{\f@size}}
\makeatother

\begin{document}

\makeatletter

\large

\ifthenelse{\f@size = \smallfontsize }{the size is small}{the size is not small}

\small

\ifthenelse{\f@size = \smallfontsize }{the size is small}{the size is not small}
\makeatother

\end{document}

enter image description here