Text FormattingOne command for "bold" both in text and math mode

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
Alextorm
Posts: 3
Joined: Sun Nov 21, 2010 7:30 pm

One command for "bold" both in text and math mode

Post by Alextorm »

How can I define one new command to have the text formatted in bold both in math and in text mode?
Last edited by Alextorm on Tue Nov 23, 2010 7:17 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

One command for "bold" both in text and math mode

Post by svend_tveskaeg »

Why a single command? I would simply use \textbf for bold text and \mathbf for bold mathematics.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
yoriz
Posts: 8
Joined: Sun Nov 21, 2010 8:39 pm

One command for "bold" both in text and math mode

Post by yoriz »

I agree it can be useful sometimes. I sometimes put the following in my preamble:

Code: Select all

\let\oldtextbf=\textbf
\renewcommand\textbf[1]{{\boldmath\oldtextbf{#1}}}
This code adds an additional \boldmath call to the \textbf command, making all math within a \textbf{} bold. Of course, if you don't want to overwrite \textbf, you can define a new command:

Code: Select all

\newcommand\allbold[1]{{\boldmath\textbf{#1}}}
Yori
Alextorm
Posts: 3
Joined: Sun Nov 21, 2010 7:30 pm

Re: One command for "bold" both in text and math mode

Post by Alextorm »

I had in mind something like an "if-then" construction ("if math environment do math bold, if text environment do text bold"), but yoriz solution is just perfect for me.

Thanks to all.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

One command for "bold" both in text and math mode

Post by localghost »

Alextorm wrote:I had in mind something like an "if-then" construction ("if math environment do math bold, if text environment do text bold"), but yoriz solution is just perfect for me.[…]
You can implement it in exactly that way.

Code: Select all

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\newcommand*{\genbf}[1]{\ifmmode\mathbf{#1}\else\textbf{#1}\fi}

\begin{document}
  \genbf{In-line text}
  \[
    \genbf{Math}
  \]
\end{document}
Post Reply