在此主题,@HenriMenke 展示了如何在符号周围放置一个圆圈。修改他的代码来给圆圈上色很容易,见下文,但我不明白如何将颜色设为可选参数,即默认为黑色,并将颜色作为附加可选参数。这通常很容易,但在这段代码中,出于某种原因,我不明白传递给的参数数量\mathcircled
与传递给的参数数量不同\@mathcircled
\usepackage{mathtools}
\usepackage{tikz}
\makeatletter
\newcommand\mathcircled[1]{%
\mathpalette\@mathcircled{#1}%
}
\newcommand\@mathcircled[2]{%
\tikz[baseline=(math.base)] \node[draw,color=red,circle,inner sep=1pt] (math) {$\m@th#1#2$};%
}
\makeatother
另一个问题是如何改变周围物体的形状。您可能认为可以用或或替换circle
作为的第三个参数,但这些选项都不起作用。我也尝试过类似但那也不起作用。感谢您的任何建议。\node
square
oval
ellipse
shape=ellipse
答案1
您可以将两个参数包装在一起:
\documentclass[]{article}
\usepackage{mathtools}
\usepackage{tikz}
\makeatletter
\newcommand\mathcircled[2][red]{%
\mathpalette\@mathcircled{{#1}{#2}}%
}
\newcommand\@mathcircled[2]{%
\@mathcircled@b{#1}#2%
}
\newcommand\@mathcircled@b[3]{%
\tikz[baseline=(math.base)] \node[draw,color=#2,circle,inner sep=1pt] (math) {$\m@th#1#3$};%
}
\makeatother
\begin{document}
$\mathcircled[blue]{55}$
\end{document}
如果没有第二个辅助宏,您也可以将\@mathcircled
和{#1}
放在一个参数中\mathpalette
:
\documentclass[]{article}
\usepackage{mathtools}
\usepackage{tikz}
\makeatletter
\newcommand\mathcircled[2][red]{%
\mathpalette{\@mathcircled{#1}}{#2}%
}
\newcommand\@mathcircled[3]{%
\tikz[baseline=(math.base)] \node[draw,color=#1,circle,inner sep=1pt] (math) {$\m@th#2#3$};%
}
\makeatother
\begin{document}
$\mathcircled[blue]{55}$
\end{document}
两个代码块的结果都是:
编辑:根据要求,版本采用了更多的参数(准确地说是 3 个)。
这种方法连续采用两个可选参数。如果第一个参数为空,则将其设置为red
(这是red
默认值,因为它默认为空)。第二个可选参数指定形状,默认为circle
。我尝试在注释中解释一下代码:
\documentclass[border=2mm]{standalone}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{shapes}
\makeatletter
\newcommand\mathcircled[1][]
{%
% keeping stuff local
\begingroup
% check whether #1 is empty
\if\relax\detokenize{#1}\relax
% save red as the colour for later
\def\mathcircled@color{red}%
\else
% save #1 as the colour for later
\def\mathcircled@color{#1}%
\fi
% grab the next optional argument and the mandatory one
\mathcircled@b
}
\newcommand\mathcircled@b[2][circle]
{%
% expand the colour and read the expanded colour and the other two arguments
\expandafter\mathcircled@c\expandafter{\mathcircled@color}{#1}{#2}%
}
\newcommand\mathcircled@c[3]
{%
% putting the two optional arguments with \mathcircled@d into the first
% argument of \mathpalette
\mathpalette{\mathcircled@d{#1}{#2}}{#3}
\endgroup
}
\newcommand\mathcircled@d[4]
{%
\tikz[baseline=(math.base)]
\node[draw,color=#1,#2,inner sep=1pt] (math) {$\m@th#3#4$};%
}
\makeatother
\begin{document}
$\mathcircled[blue]{55}$
$\mathcircled[][ellipse]{69}$
$\mathcircled{5}$
\end{document}
结果:
使用以下版本的编辑xparse
:
\documentclass[border=2mm]{standalone}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{shapes}
\usepackage{xparse}
\makeatletter
% this will be used to parse the first optional argument of \mathcircled to
% set the default.
\newcommand \EmptyArgParse [2]
{%
\if\relax\detokenize{#2}\relax
\def\ProcessedArgument{#1}%
\else
\def\ProcessedArgument{#2}%
\fi
}
\NewDocumentCommand \mathcircled { >{\EmptyArgParse{red}}O{} O{circle} m }
{%
\mathpalette{\mathcircled@b{#1}{#2}}{#3}%
}
\newcommand\mathcircled@b[4]
{%
\tikz[baseline=(math.base)]
\node[draw,color=#1,#2,inner sep=1pt] (math) {$\m@th#3#4$};%
}
\makeatother
\begin{document}
$\mathcircled[blue]{55}$
$\mathcircled[][ellipse]{69}$
$\mathcircled{5}$
\end{document}