以下是一个解决方案(由@Stefan 提供)为了带圆圈的数字在 的帮助下tikz
。我该怎么做才能使我可以就地自定义其绘制颜色、填充颜色、文本颜色等?
\documentclass[tikz,border=7pt]{standalone}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
\node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
\begin{document}
\circled{1}
\end{document}
答案1
我建议tikzmark
在这里使用,因为这个神奇的库可以检测您处于哪种模式(文本模式还是数学模式,如果是数学模式,则检测哪种模式),所以您不必关心这一点。我想说的是,应该以相对单位指定线宽,以便它与字体大小一起缩放。当然,您可以为东西着色等等。(最好的部分是您可以连接路径中的节点等等。)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\tikzset{mycircled/.style={circle,draw,inner sep=0.1em,line width=0.04em}}
\begin{document}
abc \tikzmarknode[mycircled,draw=red]{t1}{d}
{\Huge \tikzmarknode[mycircled,draw=blue,text=purple]{t1}{D}}
\[
\int\limits_{\tikzmarknode[mycircled,red]{a1}{a}}^{\tikzmarknode[mycircled,blue]{b1}{b}}
f(x)\,\mathrm{d}x=F(\tikzmarknode[mycircled,red]{a2}{a})-
F(\tikzmarknode[mycircled,blue]{b2}{b})
\]
\begin{tikzpicture}[overlay,remember picture]
\draw[latex-latex,red] (a1) to[bend right] (a2);
\draw[latex-latex,blue] (b1) to[bend left] (b2);
\end{tikzpicture}
\end{document}
答案2
根据@marmot (友善:P) 的评论进行编辑 (感谢):
这个答案确实需要注意所有字母的某种恒定大小,但不关心何时line width
增加/减少,因为我必须将这个参数设置为额外的,或者以某种方式进行一些黑客攻击,以便将其提供给我的宏,而且我不确定 OP 是否需要更改线宽(我只是最初给出它作为改善外观的想法。)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter
\newlength{\mylength}
\xdef\CircleFactor{1.1}
\setlength\mylength{\dimexpr\f@size pt}
\newsavebox{\mybox}
\newcommand*\circled[2][draw=blue]{\savebox\mybox{\vbox{\vphantom{WL1/}#1}}\setlength\mylength{\dimexpr\CircleFactor\dimexpr\ht\mybox+\dp\mybox\relax\relax}\tikzset{mystyle/.style={circle,#1,minimum height={\mylength}}}
\tikz[baseline=(char.base)]
\node[mystyle] (char) {#2};}
\makeatother
\begin{document}
This answer takes care of the current font size in a way that \circled{i} and \circled{W} will appear the same size here and in the next paragpaph with a \verb|\tiny| (or whatever font).
\noindent Demonstration:\\
\circled{i}\circled{i}\circled{W}\circled{W}\\
\circled{W}\circled{W}\circled{i}\circled{i}
\tiny This answer takes care of the current font size in a way that \circled{i} and \circled{W} will appear the same size here and in the this with a \verb|\tiny| (or whatever font).
\noindent Demonstration:\\
\noindent\circled{i}\circled{i}\circled{W}\circled{W}\\
\circled{W}\circled{W}\circled{i}\circled{i}
\normalsize Also you can give a parameter inside an optional argument of the command and have different appearance:
\circled[text=blue,fill=red,draw=black]{W}\circled[fill=yellow,draw=red,text=blue]{W}
\end{document}
这是旧的答案并没有真正关心尺寸根据我的回答这里。
您将选择样式,并可以在命令定义的可选参数中添加您喜欢的样式(我只是放置了选项draw=blue
)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter
\def\mfontsize{\f@size}
\newcommand*\circled[2][draw=blue]{\tikzset{mystyle/.style={circle,#1,minimum height={\mfontsize*1.4}}}\tikz[baseline=(char.base)]
{
\node[mystyle] (char) {\vphantom{WAH1g}#2};}}
\makeatother
\begin{document}
This is \circled{a} \circled[text=blue,fill=orange,draw=red, line width=0.3mm]{1} balloon.
\end{document}
答案3
您可以向命令添加更多参数。
梅威瑟:
\documentclass[tikz,border=7pt]{standalone}
\newcommand*\circled[4]{\tikz[baseline=(char.base)]{
\node[shape=circle, fill=#2, draw=#3, text=#4, inner sep=2pt] (char) {#1};}}
\begin{document}
\circled{1}{red}{green}{blue}
\end{document}
结果:
\NewDocumentCommand
您还可以使用包中的选项指定颜色的默认值xparse
:
\documentclass[tikz,border=7pt]{standalone}
\usepackage{xparse}
\NewDocumentCommand{\circleddefaults}{
O{black}
O{yellow}
O{orange}
m
}{
\tikz[baseline=(char.base)]{
\node[shape=circle, fill=#1, draw=#2, text=#3, inner sep=2pt] (char) {#4};}}
\newcommand*\circled[4]{\tikz[baseline=(char.base)]{
\node[shape=circle, fill=#2, draw=#3, text=#4, inner sep=2pt] (char) {#1};}}
\begin{document}
\circled{1}{red}{green}{blue}
\circleddefaults[blue,brown]{2} % two colors specified, third default
\circleddefaults{3} % all defaults
\end{document}