0

I would like to setup a command for adding inline math in text paragraphs. In that command, I want to change some style properties, like change the color of the equation. Fox example:

The value of $x = 2 + 3$ is $x=5$.

I would like to do.

 The value of \Mimath{x = 2 + 3} is \Mimath{x=5}.

I through I could start with something like this:

\newcommand*\Mimath[1]{ $ #1 $ }

But that gives an error in TexStudio:

Undefined control sequence. ...lection of objects whatsoever. A set \Mimath

How can I create that type of command?

1
  • 1
    Please write a minimal working example. See link for more details what it means.
    – user202729
    May 10, 2023 at 4:55

2 Answers 2

1

This works for me, I hope for you too:

\documentclass[12pt]{article}
\usepackage{xcolor}
\newcommand*\Mimath[2][black]{{\color{#1} $ #2 $}\color{black} }

\begin{document}
The value of    \Mimath[red]{y=x+5}

The value of    \Mimath[blue]{y=x+5}

The value of    \Mimath[magenta]{y=x+5}

The value of    \Mimath{y=x+5}
\end{document}

output

4
  • 2
    +1. I don't think that \color{black} is needed, though.
    – Mico
    May 10, 2023 at 5:30
  • I'd use: \newcommand*\Mimath[2][black]{$\color{#1}#2$}. May 10, 2023 at 5:32
  • @Mico without \color{black} does not reset the color of text to black and continues the same color of math mode
    – miltos
    May 10, 2023 at 5:43
  • Sorry, I should have written either \newcommand*\Mimath[2][black]{$\color{#1}#2$} or \newcommand*\Mimath[2][black]{{\color{#1}$#2$}}.
    – Mico
    May 10, 2023 at 5:45
0

You can use \everymath{\color{blue}}. Here is a minimimal working example:

\documentclass{amsart}
\usepackage{xcolor}

\everymath{\color{blue}}
\begin{document}

The value of $x = 2 + 3$ is $x=5$.

\[
   \int2x\,dx=x^2+C
\]

\end{document}

that produces:

enter image description here

As the MWE shows, this colours only inline mathematics. If you want to colour displayed equations as well use \everydisplay{\color{blue}}.

3
  • Running \everydisplay{\color{blue}} can cause unexpected results, to put it mildly. Running \everydisplay=\expandafter{\the\everydisplay\color{blue}} is safer.
    – Mico
    May 10, 2023 at 5:35
  • @Mico Really? I have not come across this. Is there a reference about this that I can look at or some examples? May 10, 2023 at 5:37
  • 2
    The issue is that \everydisplay{\color{blue}} clobbers any preexisting token list, which may have been set up by some package the user has loaded. For a practical example of what can go wrong see, e.g., Modifying \everydisplay causes the align* environment to stop working.
    – Mico
    May 10, 2023 at 5:43

You must log in to answer this question.

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