1

I have a macro that occurs very frequently in the text. At one occasion, it occurs inside a heading, which causes some problems:

\documentclass{scrartcl}
\newcommand\FrequentlyUsedMacro{\emph{\textbf{Macro}}}
\begin{document}

\section{Random section}
This section has the preferred font in its title.\\
Also, very many \FrequentlyUsedMacro{}s.

\section{\FrequentlyUsedMacro{}}
This section also has the right font, but causes warnings.

\section{\textrm{\FrequentlyUsedMacro{}}}
This removes the warnings, but the font is now wrong.

\end{document}

This outputs the following:

Latex output with different heading font styles

Section 2 gives the following warnings:

LaTeX Font Warning: Font shape `OT1/cmss/bx/it' undefined
LaTeX Font Warning: Some font shapes were not available, defaults substituted.

Why is there a warning if it is obviously possible in section 3? Or does it just not have an italic sans-serif font?

Additional information: if {article} is used instead of {scrartcl}, the warnings are gone and both headings 2 and 3 are italic. Also, the problem stays the same if \textbf{} is removed from the macro.

This is a part of a larger document so I would like to get rid of the warnings. Is there a way to keep the headings both style- and warning-free? I am quite sure there is, but I could not find it via the search function.

Any help much appreciated!

  • You answered the question basically yourself: The font just don't have bold italic sans-serif characters. The change from KOMA to article and vice-versa is change of font families too. You would get the warning in article class as well if you \renewcommand{\rmdefault}{\sfdefault}. – user31729 Jul 10 '15 at 13:39
  • @ChristianHupfer Yes, but there surely is a way to say "I'd like to have this macro without any of the pre-defined font styles", right? I thought, \textrm{} does this, but apparently not... – Brokenmind Jul 10 '15 at 13:40
  • 1
    You can change the rules for when a font is not available. Which output, or better behaviour, are you expecting to happen? – Johannes_B Jul 10 '15 at 13:41
  • 1
    \textrm changes back to Roman family, which isn't sans serif of course. You could use lmodern package. The warning will vanish then completely, regardless, which \rmdefault you use – user31729 Jul 10 '15 at 13:43
  • 2
    Once again the question: What do expect? You are writing in the question that the font is wrong. It is not, it is the font that is substituted by default if the cont demanded does not exist. – Johannes_B Jul 10 '15 at 13:50
3

I think the question is not clear enough. You are having a problem which you don't completely understand and you are asking for a particular solution that might not be what you really want. So here are a few shots in case one is exactly what you are looking for.

The problem is that there's no sans serif bold italic in that particular font that comes by default, Computer Modern Sans Serif (may be there isn't even an italic non-bold font).

You said that with article class the problem goes away, “the warnings are gone and both headings 2 and 3 are italic”. If that means that you don't really care about the headings being sans serif (which is the default in scrartcl), then

\addtokomafont{disposition}{\rmfamily}

should solve your problems.

If you do want bold sans serif in the title, then that sentence I quoted is wrong, it doesn't solve your problem just using article. In that case, do you want to have the \verycommonmacro to output in roman bold italic or in sans serif bold italic. In the first case you can redefine the macro

\newcommand*\verycommonmacro{\textrm{\emph{\textbf{Macro}}}}

or, if you want it to output sans serif bold italic, you need a font that has that particular shape. As Christian suggested, lmodern provides that.

\usepackage{lmodern}

(But you say that you get everything in italic? I don't know then what your problem is.)

The last option is what you asked, to strip away any formatting from that macro. (I forgot the code for this one.) In that case, you can set a flag inside of titles, and then let the macro check that flag, and, in case it's ON do something and if it's not do something else. In this case, with the help of etoolbox, if it's in a title it inserts \relax and if it's not, it uses \emph.

\usepackage{etoolbox}
\newtoggle{insection}
\addtokomafont{disposition}{\toggletrue{insection}}
\newcommand*\verycommonmacro
  {\iftoggle{insection}{Macro}{\emph{\bfseries Macro}}}

 So, what exactly do you want?

enter image description here

  • Wow, thanks for your detailed answer! The way I would prefer it actually is number 4. As for the article class, “the warnings are gone and both headings 2 and 3 are italic”, but the problem does not go away because italic is not what I wanted. It seems I made some grave mistakes in formulating the question... Solution number 4 is the section with the etoolbox, right? – Brokenmind Jul 10 '15 at 22:10
  • Yep, solution 4 is the code with etoolbox I just showed a basic example with \iftoggle, you can play with it in whatever way you want. Its syntax is \iftoggle{insection}{<code that appears if we are inside a section>}{<else>} so after that macro expands you get \relax{\bfseries Macro} in a section and \emph{\bfseries Macro} in any other place. – Manuel Jul 10 '15 at 22:16
  • Thanks for the explanation, I'm not very experienced with this type of commands. In any case, that's way more than I hoped for (I thought I would need to deactivate the style manually in the section header), so I'll gladly accept this as the answer. One last question (just curious): Is something like \iftoggle{insection}{\relax}{\emph\bfseries}{Macro} possible? – Brokenmind Jul 11 '15 at 9:10
  • May be clearer to you is \iftoggle{insection}{Macro}{\emph{\bfseries Macro}}, what I used is some sort of hackish shortcut. You can't do what you are doing, because \emph takes an argument. What I did was \iftoggle{insection}{\relax}{\emph} which in turn, expands to whatever but the next thing is a group {\bfseries Macro} which becomes the argument of \emph (if we are out of a header). TL;DR: I would use a clearer definition if I were you: \iftoggle{insection}{Macro}{\emph{\bfseries Macro}}. – Manuel Jul 11 '15 at 11:08
  • 1
    Now that you point it out like this, it seems kind of obvious. That version is indeed much clearer; I will go with it. – Brokenmind Jul 11 '15 at 11:22
1

Here is another suggestion using \newkomafont to define a font style for this element. This also works if there is a table of contents.

\documentclass{scrartcl}
\newkomafont{frequentlyusedmacro}{\bfseries\itshape}
\newcommand*\FrequentlyUsedMacro{{\protect\usekomafont{frequentlyusedmacro}Macro}}

\addtokomafont{disposition}{\setkomafont{frequentlyusedmacro}{}}
\BeforeStartingTOC{\setkomafont{frequentlyusedmacro}{}}

\begin{document}
\tableofcontents
\section{Random section}
This section has the preferred font in its title.\\
Also, very many \FrequentlyUsedMacro{}s.
\section{\FrequentlyUsedMacro{}}
This section also has the right font, but causes warnings.
\section{\FrequentlyUsedMacro}
\subsection{\FrequentlyUsedMacro}
This removes the warnings, but the font is now wrong.
\end{document}

enter image description here

0

I found a few solutions which I do not think are very clean. However, they might suit your needs:

\documentclass{scrartcl}
\newcommand\FrequentlyUsedMacro{\emph{\textbf{Macro}}}

\newcommand{\startnoemph}{\let\emph\relax}

\begin{document}

\section{\startnoemph \FrequentlyUsedMacro{}}
This works with \FrequentlyUsedMacro{}.

\end{document}

Or:

\documentclass{scrartcl}
\newcommand\FrequentlyUsedMacro{\emph{\textbf{Macro}}}

\newcommand{\noemph}[1]{\begingroup\let\emph\relax#1\endgroup}

\begin{document}

\section{\noemph{\FrequentlyUsedMacro{}}}
This works with \FrequentlyUsedMacro{}.

\end{document}

I'm sure there is a possibility to use the package etoolbox to patch \section so that this works automatically.

  • As you assumed, there was some way to do it with the etoolbox package, and it appears to be cleaner... Nonetheless, I'm sure I encounter some situations where your code comes in handy, so thanks for pointing it out! – Brokenmind Jul 11 '15 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.