15

Here's what my equation looks like; it's in an align* environment:

equation

I want to put a circle around $\varepsilon \frac{dL}{d\varepsilon}$ and everything else unchanged.

Q: How do I draw a circle around a term in an align* equation?

I found that \textcircled doesn't work in math mode and \tikz \node[circle,draw] { $\varepsilon \frac{dL}{d\varepsilon}$}; is not suitable as it shifts the term:

enter image description here

I get fairly close using \boxed{\varepsilon \frac{dL}{d\varepsilon}}, but it's not a circle:

enter image description here

Here's a working example:

\documentclass{article}

\usepackage{amsmath,amssymb}

\begin{document}

\begin{align*}
L(q+\varepsilon) = L(q) + \varepsilon \frac{dL}{d\varepsilon} + \cdots \\
\end{align*}

\end{document}
Henri Menke
  • 109,596
Rebecca J. Stones
  • 1,439
  • 2
  • 11
  • 22

1 Answers1

22

The TikZ-way you tried works, you just forgot to set the baseline of the tikzpicture to the baseline of the text inside the math node. Here is an adjusted solution which also respects the mathstyle outside of the node.

\documentclass{article}
\usepackage{mathtools}% superior to amsmath
\usepackage{tikz}
\makeatletter
\newcommand\mathcircled[1]{%
  \mathpalette\@mathcircled{#1}%
}
\newcommand\@mathcircled[2]{%
  \tikz[baseline=(math.base)] \node[draw,circle,inner sep=1pt] (math) {$\m@th#1#2$};%
}
\makeatother
\begin{document}

\begin{align*}
  L(q+\varepsilon)
  &= L(q) + \mathcircled{\varepsilon \frac{dL}{d\varepsilon}} + \cdots \\
  &= \textstyle L(q) + \mathcircled{\varepsilon \frac{dL}{d\varepsilon}} + \cdots \\
  &= \scriptstyle L(q) + \mathcircled{\varepsilon \frac{dL}{d\varepsilon}} + \cdots \\
  &= \scriptscriptstyle L(q) + \mathcircled{\varepsilon \frac{dL}{d\varepsilon}} + \cdots
\end{align*}

\end{document}

enter image description here

Henri Menke
  • 109,596
  • Excellent. Thanks, that's exactly what I'm after. – Rebecca J. Stones Jun 11 '15 at 06:22
  • 1
    What is the meaning of $\m@th#1#2$. Would you please explain the code. – Fadwa Nov 18 '15 at 19:28
  • 2
    @Misaki The definition is \m@th: macro:->\mathsurround \z@. That is to say, it sets \mathsurround to 0pt. – Henri Menke Nov 18 '15 at 20:00
  • I am trying to add two circles instead of one, i used your code and add one in another \mathcircled{\mathcircled{\varepsilon \frac{dL}{d\varepsilon}}}, but the spacing between them is large besides i can't change the color of the other. would you please help. Thanks – Fadwa Nov 19 '15 at 05:21
  • @Misaki Instead of nesting, use this definition of \dblmathcircled: \newcommand\dblmathcircled[1]{\mathpalette\@dblmathcircled{#1}} \newcommand\@dblmathcircled[2]{\tikz[baseline=(math.base)]\node[draw,circle,double,inner sep=1pt] (math) {$\m@th#1#2$};}. – Henri Menke Nov 19 '15 at 09:25
  • U defined tikz double error. – Fadwa Nov 19 '15 at 10:59
  • Very nice solution as far as I understand it! It would be helpful if you added how the macro, \mathpalette and tikz commands work. I was able to figure it out at a fairly high level using chatGPT. – John Glen Mar 20 '24 at 19:32