0

i made changes in my tex files, so I am looking for a textbf gray. Now the new command created do not breake lines at the end of the document. (same problem as \colorbox{}{})

I found this: (from: Change general color when using \textbf{})

  • \let\oldtextbf\textbf \renewcommand{\textbf}[1]{
    \textcolor{gray}{\oldtextbf{#1}} }

but had an issue with math mode cause i did a lot of this: $ \textbf{something} $

My tex code is very very large so i don't want to change to this: $ \text{ \textbf{something} } $

So i changed the newcommand above, and solve the previous issue:

  • \let\oldtextbf\textbf \renewcommand{\textbf}[1]{ \text{ \textcolor{gray}{\oldtextbf{#1}} } }

MY (real) PROBLEM IS: now, the new \textbf{} command do not 'breake lines' when arrives at the end of the document, so the line go out the document.

Why? what argument i missed? is there a way to specify a command that will break line?

(sorry for my english, i am going to edit the text at some moment)

4
  • You can use \color instead of \textcolor, which is a switch that changes the color permanently until the next switch, the end of the group, or the end of the document. Here you can use the 'end of the group' option: \newcommand{\boldgray}[1]{\begingroup\color{gray}\textbf{#1}\endgroup}.
    – Marijn
    Dec 7, 2023 at 11:53
  • This is probably a duplicate, I'll see if I can find it.
    – Marijn
    Dec 7, 2023 at 11:54
  • 1
    \text forms a box, boxes are not line breakable. You could use \renewcommand\textbf[1]{\relax\ifmmode\mathcolor{gray}{\textbf{#1}}\else\textcolor{gray}{\textbf{#1}}\fi instead.
    – Skillmon
    Dec 7, 2023 at 11:56
  • Ah I see, I missed the math mode part of the question :o My version also works for math by either putting the argument in math mode separately ($a+$\boldgray{$b^2$}$+c$) which is inconvenient and disrupts kerning, or by using \mathbf in the definition, which causes it not to work in text mode anymore - so the version by @Skillmon is better as it covers both cases.
    – Marijn
    Dec 7, 2023 at 12:03

2 Answers 2

6

The font commands have hooks. E.g. to add something to bold you can use the bfseries hook:

\documentclass{article}

\usepackage{xcolor}
\AddToHook{bfseries}{\color{gray}}

\begin{document}
This is some very long text that should hit the end of the line \textbf{but in
gray and bold and very elegant} no longer gray.

$\textbf{Example}$. no longer gray
\end{document}

enter image description here

2

Instead of unconditionally boxing the contents up in \text you could check whether or not you're in math-mode and use \mathcolor if that's the case, instead \textcolor.

\documentclass{article}

\usepackage{xcolor}

\NewCommandCopy\oldtextbf\textbf
\renewcommand\textbf[1]{\relax\ifmmode\mathcolor{gray}{\oldtextbf{#1}}\else\textcolor{gray}{\oldtextbf{#1}}\fi}

\begin{document}
This is some very long text that should hit the end of the line \textbf{but in
gray and bold and very elegant}.

$\textbf{Example}$.
\end{document}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .