7

I have a long document which includes quite a lot of \textbf{...} and \textit{...} even just in the middle of sentences. I would like to put something in the preamble in order to make every text that is bold or italic, a sans serif text. I need it to be reversible (that's why I don't want to change each of the hundreds of occurrences adding \sffamily{...}).

I already tried to use

\let\oldtextbf\textbf  
\renewcommand{\textbf}{\sffamily\oldtextbf}
\let\oldtextit\textit
\renewcommand{\textit}{\sffamily\oldtextit}

but this only produces sans serif from the first occurrence until the end of the document. What should I use to get back to normal font every time a bold or italic text ends?

14

LaTeX already has the infrastructure to do it:

\documentclass{memoir}
\usepackage{lmodern}

\DeclareTextFontCommand{\textbf}{\sffamily\bfseries}
\DeclareTextFontCommand{\textit}{\sffamily\slshape}

\begin{document}

Now is \textbf{the time} for \textit{all good} men...

\end{document}

enter image description here

Note that the default definition of \textbf is

\DeclareTextFontCommand{\textbf}{\bfseries}

and that it's definitely not a good idea to redefine \textbf using \renewcommand, which would make fragile commands. With the above approach, that's reversed by simply commenting out the two lines, the commands \textbf and \textit remain robust.

However, the best would probably be defining personal commands to use instead of \textbf and \textit:

\DeclareTextFontCommand{\bfemph}{\sffamily\bfseries}
\DeclareTextFontCommand{\itemph}{\sffamily\slshape}
%\DeclareTextFontCommand{\bfemph}{\bfseries}
%\DeclareTextFontCommand{\itemph}{\itshape}

Switch the comments to use one version or the other and use \bfemph and \itemph in your document.

  • Just out of curiosity, is there a way to define commands that will inherit the font family from the surrounding text, as many other markup languages do? – Random832 May 27 '15 at 19:54
  • @Random832 Not really clear, I'm afraid. Can you explain? – egreg May 27 '15 at 19:58
  • nevermind, I think I misunderstood something. – Random832 May 27 '15 at 20:00
  • @Random832 That's the (unconfigurable) default in TeX: if you want a font change, you need to tell it to change fonts. Also, as a note to OP, I would suggest using more meaningful names than \bfemph and \itemph. That doesn't give any indication to the reader about what those typographic patterns mean. – Sean Allred May 28 '15 at 4:59

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.