23

When using \textbf{text}, the length of text will be increased. Is there a way to make the text bold face without increasing its length?

edit:

Thanks to @Herbert and @Martin, here is a perfect solution for me:

   \newsavebox\CBox 
   \newcommand<>*\textBF[1]{\sbox\CBox{#1}\resizebox{\wd\CBox}{\ht\CBox}{\textbf#2{#1}}}
Li Wang
  • 1,013

5 Answers5

18
\documentclass{article}
\usepackage{graphicx}
\newsavebox\CBox
\def\textBF#1{\sbox\CBox{#1}\resizebox{\wd\CBox}{\ht\CBox}{\textbf{#1}}}
\parindent=0pt
\begin{document}
foobarbaz y\\
\textbf{foobarbaz} y\\
\textBF{foobarbaz} y

\end{document}

enter image description here

The example for beamer

\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\newsavebox\CBox
\newcommand<>*\textBF[1]{\only#2{\sbox\CBox{#1}\resizebox{\wd\CBox}{\ht\CBox}{\textbf{#1}}}}
\parindent=0pt
\begin{document}
\begin{frame}{foo}
\only<1>{foobarbaz y1} 
\textbf<2->{foobarbaz y2}
\textBF<3>{foobarbaz y3}
\end{frame}

\end{document}

Another (better) possibility is to use the letter spacing option from the package microtype

Moriambar
  • 11,466
  • is it possible to make it work with beamer overlays? --e.g.\textBF<1>{text} – Li Wang Jul 21 '11 at 10:06
  • use it this way: \only<1>{\textBF{text}} or put this definition into \def\textBFitself. –  Jul 21 '11 at 10:28
  • It should be \newcommand<>\textBF[1]{\only#2{\sbox\CBox...}}, where the dots mean all the rest in Herbert's definition. – egreg Jul 21 '11 at 10:33
  • @Herbert: Humm, using \only here will lead to a different effect in my situation --- i.e. the text will disappear in other slides. – Li Wang Jul 21 '11 at 10:36
  • @Lee: then you do not have a \only<1-> so that it appears from slide 1 up to the end. Right? –  Jul 21 '11 at 10:40
  • @Herbert: humm, I do not fully understand the usage of \only. How could I make \only<3>{\textbf{text}} works like \textbf<3>{text}? – Li Wang Jul 21 '11 at 10:48
  • @egreg: sorry for missing your reply. Could you please write a full solution? I couldn't make it to work. Thank you very much. – Li Wang Jul 21 '11 at 11:06
  • @Lee: \newsavebox\CBox \newcommand<>*\textBF[1]{\sbox\CBox{#1}\resizebox{\wd\CBox}{\ht\CBox}{\textbf#2{#1}}} works for me. There was some funny invisible unicode character in the code of my last comment. It might have caused that (Herbert: Feel free to add this code to your answer) – Martin Scharrer Jul 21 '11 at 11:07
  • Martin and Herbert: thank you soooooooooooooooooo much! A perfect solution for me:) – Li Wang Jul 21 '11 at 11:13
8

You can use \fontseries{b}\selectfont to select a non-extended bold font, provided of course that the font family you're using contains such a font (as computer modern does). For comparison, note that \textbf uses \fontseries{bx}, where the xstands for “extended”.

  • In my opinion, the visual difference between Computer Modern's medium and non-extended-bold fonts isn't all that big. As a result, viewers of a beamer presentation might not even notice the intended effect of alternating between those two fonts. – Mico Jul 21 '11 at 10:41
  • @Mico: You have a point there. I was in a hurry and hadn't noticed that this was for a beamer presentation (which wasn't clear from the question itself). But I'll let the answer stand for future reference, since this is an option that should at least be considered. – Harald Hanche-Olsen Jul 21 '11 at 11:15
  • I agree, it's good to mention the option of using a non-extended bold form of the Computer Modern roman (serif) fonts. – Mico Jul 21 '11 at 11:51
3

This answer is just for adding overlay support to Herber's command, who should credited for it.

\newcommand<>\textBF[1]{\only#2{\dotextBF}{#1}}
\newcommand\dotextBF[1]{\sbox0{#1}\resizebox{\wd0}{\ht0}{\textbf{#1}}}

...

\textBF<2->{xyz}

"xyz" will be bold from the second screen onwards.

I don't think it's a good way to emphasize things on screen; colors are better.

egreg
  • 1,121,712
2

You could use the following macro instead. It uses the size of the horizontal and vertical size of the normal text, but sets it as bold text which makes the bold text lap over the text which follows. The text is also not breakable any longer. For overlays of table cells this should be fine but I wouldn't use it in running text of course.

\newcommand*{\textBF}[1]{\rlap{\raisebox{0pt}[0pt][0pt]{\textbf{#1}}}\phantom{#1}}

If you want to support beamer overlays use:

\newcommand<>*{\textBF}[1]{\only#2{\rlap{\raisebox{0pt}[0pt][0pt]{\textbf{#1}}}\phantom}{#1}}

or

\newcommand<>*{\textBF}[1]{\alt#2{\rlap{\raisebox{0pt}[0pt][0pt]{\textbf{#1}}}\phantom{#1}}{\mbox{#1}}}
Martin Scharrer
  • 262,582
2

If you're willing to (or allowed to) switch to typewriter (monospaced) font, you won't encounter the problem: the regular and bold fonts have glyphs of the exact same widths.

The following MWE uses Courier (scaled 5% in order to equate the cap-heights of the mono and text fonts in use), because in that font the difference between medium weight and bold is particularly strong. The lmodern package has a bold mono font as well, but the difference to the medium weight isn't as pronounced.

\documentclass{article} 
\usepackage[scaled=1.05]{couriers} %%
\begin{document}
\noindent
\texttt{The quick brown fox 012456789}\\
\texttt{\bfseries The quick brown fox 012456789}
\end{document}

Another font package possibility you may want to consider is arev. (Given that you seem to be preparing for a beamer presentation, using the arev package would be a good choice anyway.) In the arev package, the mono font harmonizes well with the text font (which is a sans-serif font), and the medium and bold mono fonts are quite different.

David Carlisle
  • 757,742
Mico
  • 506,678