Since most classes define their own option handling system you need one different macro for each.
For example, for 12pt
, article
(and the standard LaTeX classes) store just 2
in \@ptsize
. extarticle
stores 12
. beamer
stores {size12.clo}
in a \beamer@size
macro, and memoir
stores 12
in \@memptsize
. Not exactly standardised.
Here's a macro that checks for these classes and uses the correct macro accordingly, then stores the font size (from the option, not the actual font size) in a length:
\documentclass[14pt]{extarticle}
\makeatletter
\newcommand{\deffontsize}[1]{%
\setlength#1%
{%
\vincent@ifclassloaded {beamer}
\get@beamersize
{%
\vincent@ifclassloaded {memoir}
\get@memoirsize
{%
\vincent@ifclassloaded {extarticle}
\get@extartsize
\get@standardsize
}%
}%
}%
}
\def\vincent@ifclassloaded#1{%
\expandafter\ifx\csname ver@#1.cls\endcsname\relax
\expandafter\@secondoftwo
\else
\expandafter\@firstoftwo
\fi}
\def\get@beamersize{%
\expandafter\expandafter\expandafter\get@@beamersize
\expandafter\@firstofone\beamer@size}
\def\get@@beamersize size#1.clo{#1 pt}
\def\get@memoirsize{\@memptsize pt}
\def\get@extartsize{\@ptsize pt}
\def\get@standardsize{1\@ptsize pt}
\makeatother
\begin{document}
\newlength\mainfontsize
\deffontsize{\mainfontsize}
\the\mainfontsize
\end{document}
For other classes you'd need to add another conditional and the corresponding macro to get the option.