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

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

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:

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

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}


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}


• Excellent. Thanks, that's exactly what I'm after. – Rebecca J. Stones Jun 11 '15 at 6:22
• What is the meaning of $\m@th#1#2$. Would you please explain the code. – Fadwa Nov 18 '15 at 19:28
• @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 5: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 9:25