Text FormattingOverriding fontsize in macro

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
jm.meyer
Posts: 4
Joined: Mon Nov 09, 2009 5:25 pm

Overriding fontsize in macro

Post by jm.meyer »

Hi,
is it somehow possible to override the style of a macro's output, e.g. its fontsize ?

Code: Select all

\documentclass{article}
\newcommand{\helloworld}{{\large\textbf{Hello World!}}}
\begin{document}
\begin{normalsize}
\helloworld

\\Above message is too large! It doesn't work this way, but how?
\end{normalsize}
\end{document}

In the above example, i want the size of "Hello World" to be normalsize (or anything else but large) aswell.
As i understand the macro is expanded when compiling, therefore the fontsize definded in the macro overrides the "normalsize"-environment the macro was used in. Is there a way of forcing the another fontsize to be applied to the macros output? (Extrakting a string from the macro would work for me too)
Thanks in advance
JaN

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX books
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Overriding fontsize in macro

Post by Juanjo »

It is not clear from your post why are facing such a question. To avoid your problem, don't define macros with fontsize specifications inside. Anyway, an alternative would be to be put fontsize commands in an optional argument, or just deactivate them before expanding the macro, as shown below:

Code: Select all

\documentclass{article}
\newcommand{\helloworld}{{\large\textbf{Hello World!}}}
\newcommand{\HelloWorld}[1][\large]{\textbf{#1Hello World!}}
\begin{document}
\begin{normalsize}
{\let\large\relax \helloworld}

Above message is too large! It doesn't work this way, but how?

\HelloWorld

\HelloWorld[]

\end{normalsize}
\end{document}
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
jm.meyer
Posts: 4
Joined: Mon Nov 09, 2009 5:25 pm

Overriding fontsize in macro

Post by jm.meyer »

I'm facing this problem since i want to use "\ref{subsec:foo}" to refer to a "\label{subsec:foo}" that labels a "\subsection{Foo}". Im using \documentclass{scrreprt} from KOMA Script and altered the fontsize of the section-number via:

Code: Select all

\renewcommand{\thesubsection}{\large\arabic{section}.\normalsize\arabic{subsection}\,}
As it seems the output of "\ref" includes the fonsize specifications as made above. So i get a large sectionnumber anytime i refer to a subsection, but i want it to be normalsized in the continous text.

But thank you for your answer anyway Juanjo,i learned something about optional arguments that way! I guess i should have come up with my actual problem right away, i caught that general-solution-illnes from my major, which is physics...
Post Reply