是否可以建立基於 Tikz 的文字?

是否可以建立基於 Tikz 的文字?

在我的 Tikz 圖中,我創建了一個用紅色填滿的簡單圓圈:

\addplot [mark=*, mark size=3, mark options={solid, fill=red}] coordinates {
(1.25, 0) };

現在我想在文字中引用該圓圈,所以我想知道是否可以建立一個命令,當與文字內聯時,再現相同的圓圈。

編輯

可能的解決方案幾乎適用於圖標題:

在此輸入影像描述

答案1

TikZ中也可以畫圓;plot也接受mark選項:

\documentclass{article}
\usepackage{tikz}
\newcommand*{\RedCircle}{}
\DeclareRobustCommand*{\RedCircle}{%
  \tikz\path plot[
    mark=*,
    mark size=3,
    mark options={solid, fill=red},
  ] coordinates {(0, 0)};%
}
\begin{document}
Circle: \RedCircle
\end{document}

結果

圍繞數學軸垂直居中

居中的符號更適合放在括號中。

\documentclass{article}
\usepackage{tikz}
\newcommand*{\RedCircle}{}
\DeclareRobustCommand*{\RedCircle}{%
  \ensuremath{\vcenter{\hbox{%
    \tikz\path plot[
      mark=*,
      mark size=3,
      mark options={solid, fill=red},
    ] coordinates {(0, 0)};%
  }}}%
}
\begin{document}
Circle (\RedCircle)
\end{document}

結果

邊界框校正

TikZ 似乎忘記在邊界框中考慮標記的線寬。以下程式碼新增了一個小框架來補償這一點:

\documentclass{article}
\usepackage{tikz}
\newcommand*{\RedCircle}{}
\DeclareRobustCommand*{\RedCircle}{%
  \ensuremath{\vcenter{\hbox{%
    \setlength{\fboxsep}{.21pt}%
    % 0.2pt (half line width)
    % + 0.01pt to get some rounding tolerance
    \setlength{\fboxrule}{0pt}%
    \fbox{%
      \tikz\path plot[
        mark=*,
        mark size=3,
        mark options={solid, fill=red, draw=black},
      ] coordinates {(0, 0)};%
    }%
  }}}%
}
\begin{document}
% Show bounding box:
\setlength{\fboxsep}{0pt}
\setlength{\fboxrule}{.1pt}
(\textcolor{cyan}{\fbox{\RedCircle}})
\end{document}

在 TikZ 內添加邊距(也許,圖像因外部化而被剪裁):

\documentclass{article}
\usepackage{tikz}
\newcommand*{\RedCircle}{}
\DeclareRobustCommand*{\RedCircle}{%
  \ensuremath{\vcenter{\hbox{%
    \def\BoundingBoxCorrection{.55\pgflinewidth}%
    % .5\pgflinewidth: half the line width, forgotten by TikZ
    % .05\pgflinewidth: some tolerance for rounding errors
    \tikz\path plot[
        mark=*,
        mark size=3,
        mark options={solid, fill=red, draw=black},
      ] coordinates {(0, 0)}
      (current bounding box.south west)
      ++(-\BoundingBoxCorrection, -\BoundingBoxCorrection)
      (current bounding box.north east)
      ++(\BoundingBoxCorrection, \BoundingBoxCorrection)
    ;%
  }}}%
}
\begin{document}
% Show bounding box:
\setlength{\fboxsep}{0pt}
\setlength{\fboxrule}{.1pt}
(\textcolor{cyan}{\fbox{\RedCircle}})
\end{document}

結果

相關內容