[Tex/LaTex] How to draw a circle around a term in an align* equation

aligncirclessymbolstikz-pgf

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}

Best Answer

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