旧答案:有时有效

旧答案:有时有效

基于这个问题很好及其答案,我有这段小代码:

%
% circled steps (with labels to reference them!)
%
\newcommand*\circled[2][]{\tikz[baseline=(char.base)]{
            \node[shape=circle,draw,inner sep=1pt,#1] (char) {#2};}}
\newcounter{cstepcnt}
\newcommand{\startcstep}{\setcounter{cstepcnt}{0}}
\newcommand{\cstep}{%
    \refstepcounter{cstepcnt}%
    \circled[red, font=\scriptsize]{\arabic{cstepcnt}}% this is pretty dangerous
}

这样我就可以用小圆圈数字标记部分文本、方程式或其他任何内容,以便以后参考(使用\label\ref),例如:

在此处输入图片描述

这个很好,是一个比以前更好的解决方案\textcircled{}(数字居中不好,特别是如果它们大于 9,并且形状或颜色没有灵活性),但我知道当我将它用到图片中时,这是很危险的;有时它可以工作,有时它会爆炸(因为,我知道,你永远不应该嵌套tikzpictures)。

有没有办法获得同样的灵活性?我的意思是,有一种可以在 tikzpictures 内部和外部使用的“带圆圈的数字步骤”?

这是一个 MWE,其中一切都正常运转,因为我找不到它会中断的简单示例...但它有时会中断,产生非常大或非常小的圆圈。

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
%
\newcommand*\circled[2][]{\tikz[baseline=(char.base)]{
            \node[shape=circle,draw,inner sep=1pt,#1] (char) {#2};}}
\newcounter{cstepcnt}
\newcommand{\startcstep}{\setcounter{cstepcnt}{0}}
\newcommand{\cstep}{%
    \refstepcounter{cstepcnt}%
    \circled[red, font=\scriptsize]{\arabic{cstepcnt}}% this is pretty dangerous
}
\renewcommand{\thecstepcnt}{\textbf{\arabic{cstepcnt}:}}
\begin{document}
one text here \cstep{} some text here \cstep\label{c:one}.

\begin{tikzpicture}
    \draw (0,0) node[draw](A){text\cstep\label{c:inner}};
\end{tikzpicture}

And then ref~\ref{c:one} and \ref{c:inner}.
\end{document}

在此处输入图片描述

答案1

根据您的反馈修改了版本。这是对答案的尝试,但不能保证它有效。为什么不应该嵌套tikzpictures?主要原因是环境的 pgf 键tikzpicture会影响内部形状。避免这种情况的一种方法是创建“新”形状,这些形状仅在pgf使用的键上与标准形状不同。所以我ellipse从 复制了形状pgflibraryshapes.geometric.code.tex,删除了一些 pgf 键,内部 sep 现在位于目录中,Rmano以便您仍然可以在需要时从外部更改它。此形状在 pgf 级别使用。没有像 Ti 这样的中介Z\node命令可能会使事情变得模糊。不幸的是,要实现它需要付出更多努力baseline

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\makeatletter
\pgfdeclareshape{stubborn ellipse}
%
% Draws a circle around the text
%
{%
  \savedanchor\centerpoint{%
    \pgf@x=.5\wd\pgfnodeparttextbox%
    \pgf@y=.5\ht\pgfnodeparttextbox%
    \advance\pgf@y by-.5\dp\pgfnodeparttextbox%
  }%
  \savedanchor\radius{%
    %
    % Calculate ``height radius''
    %
    \pgfmathsetlength\pgf@yb{\pgfkeysvalueof{/Rmano/inner ysep}}%
    \advance\pgf@y by\pgf@yb%
    %
    % Calculate ``width radius''
    %
    \pgf@x=.5\wd\pgfnodeparttextbox%
    \pgfmathsetlength\pgf@xb{\pgfkeysvalueof{/Rmano/inner xsep}}%
    \advance\pgf@x by\pgf@xb%
    %
    % Adjust
    %
    \pgf@x=1.4142136\pgf@x%
    \pgf@y=1.4142136\pgf@y%
  }%

  %
  % Anchors
  %
  \anchor{center}{\centerpoint}%
  \anchor{mid}{\centerpoint\pgfmathsetlength\pgf@y{.5ex}}%
  \anchor{base}{\centerpoint\pgf@y=0pt}%
  \anchor{north}
  {
    \pgf@process{\radius}
    \pgf@ya=\pgf@y%
    \pgf@process{\centerpoint}
    \advance\pgf@y by\pgf@ya
  }%
  \anchor{south}
  {
    \pgf@process{\radius}
    \pgf@ya=\pgf@y%
    \pgf@process{\centerpoint}
    \advance\pgf@y by-\pgf@ya
  }%
  \anchor{west}
  {
    \pgf@process{\radius}
    \pgf@xa=\pgf@x%
    \pgf@process{\centerpoint}
    \advance\pgf@x by-\pgf@xa
  }%
  \anchor{mid west}
  {%
    \pgf@process{\radius}
    \pgf@xa=\pgf@x%
    \pgf@process{\centerpoint}
    \advance\pgf@x by-\pgf@xa%
    \pgfmathsetlength\pgf@y{.5ex}
  }%
  \anchor{base west}
  {%
    \pgf@process{\radius}
    \pgf@xa=\pgf@x%
    \pgf@process{\centerpoint}
    \advance\pgf@x by-\pgf@xa%
    \pgf@y=0pt
  }%
  \anchor{north west}
  {
    \pgf@process{\radius}
    \pgf@xa=\pgf@x%
    \pgf@ya=\pgf@y%
    \pgf@process{\centerpoint}
    \advance\pgf@x by-0.707107\pgf@xa
    \advance\pgf@y by0.707107\pgf@ya
  }%
  \anchor{south west}
  {
    \pgf@process{\radius}
    \pgf@xa=\pgf@x%
    \pgf@ya=\pgf@y%
    \pgf@process{\centerpoint}
    \advance\pgf@x by-0.707107\pgf@xa
    \advance\pgf@y by-0.707107\pgf@ya
  }%
  \anchor{east}
  {%
    \pgf@process{\radius}
    \pgf@xa=\pgf@x%
    \pgf@process{\centerpoint}
    \advance\pgf@x by\pgf@xa
  }%
  \anchor{mid east}
  {%
    \pgf@process{\radius}
    \pgf@xa=\pgf@x%
    \pgf@process{\centerpoint}
    \advance\pgf@x by\pgf@xa%
    \pgfmathsetlength\pgf@y{.5ex}
  }%
  \anchor{base east}
  {%
    \pgf@process{\radius}
    \pgf@xa=\pgf@x%
    \pgf@process{\centerpoint}
    \advance\pgf@x by\pgf@xa%
    \pgf@y=0pt
  }%
  \anchor{north east}
  {
    \pgf@process{\radius}
    \pgf@xa=\pgf@x%
    \pgf@ya=\pgf@y%
    \pgf@process{\centerpoint}
    \advance\pgf@x by0.707107\pgf@xa
    \advance\pgf@y by0.707107\pgf@ya
  }%
  \anchor{south east}
  {
    \pgf@process{\radius}
    \pgf@xa=\pgf@x%
    \pgf@ya=\pgf@y%
    \pgf@process{\centerpoint}
    \advance\pgf@x by0.707107\pgf@xa
    \advance\pgf@y by-0.707107\pgf@ya
  }%
  \anchorborder{
    \edef\pgf@marshal{%
      \noexpand\pgfpointborderellipse
      {\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}
      {\noexpand\radius}%
    }%
    \pgf@marshal%
    \pgf@xa=\pgf@x%
    \pgf@ya=\pgf@y%
    \centerpoint%
    \advance\pgf@x by\pgf@xa%
    \advance\pgf@y by\pgf@ya%
  }%

  %
  % Background path
  %
  \backgroundpath
  {
    \pgf@process{\radius}%
    \pgfutil@tempdima=\pgf@x%
    \pgfutil@tempdimb=\pgf@y%
    \advance\pgfutil@tempdima by-\pgf@xb%
    \advance\pgfutil@tempdimb by-\pgf@yb%
    \pgfpathellipse{\centerpoint}{\pgfqpoint{\pgfutil@tempdima}{0pt}}{\pgfqpoint{0pt}{\pgfutil@tempdimb}}%
  }%
}%
\pgfkeys{/Rmano/inner ysep/.initial=2pt,/Rmano/inner xsep/.initial=2pt}
%
\newcommand*\circled[2][]{\setbox0\hbox{#1}%
\pgf@xa=\ht0%
\advance\pgf@xa by \pgfkeysvalueof{/Rmano/inner ysep}%
\pgf@ya=0.4142136\pgf@xa%
\raisebox{-\pgf@ya}{\textcolor{red}{\begin{pgfpicture}%
\pgfnode{stubborn ellipse}{center}{#2}{#1}{\pgfusepath{stroke}}%
\end{pgfpicture}}}}
\makeatother
\newcounter{cstepcnt}
\newcommand{\startcstep}{\setcounter{cstepcnt}{0}}
\newcommand{\cstep}{%
    \refstepcounter{cstepcnt}%
    \typeout{\number\value{cstepcnt}}%
    \circled[cnode-\number\value{cstepcnt}]{\number\value{cstepcnt}}% this is pretty dangerous
}
\renewcommand{\thecstepcnt}{\textbf{\arabic{cstepcnt}:}}
\begin{document}
one text here \cstep{} some text here \cstep\label{c:one}.

\begin{tikzpicture}
    \draw (0,0) node[draw](A){text\cstep\label{c:inner}};
\end{tikzpicture}

And then ref~\ref{c:one} and \ref{c:inner}.
\foreach \X in {1,...,12} {\cstep{}}.
\end{document}

在此处输入图片描述

(看到这个\raisebox黑客行为,人们真的很高兴 TiZ 带有简单的按键,baseline可以让人避免所有这些混乱。;-)

答案2

确实,安全嵌套似乎几乎是不可能的pgfpictures(正如到处都明确指出的那样)。因此,我实施了一种不同的策略,您可以在原始问题的答案中看到:https://tex.stackexchange.com/a/496665/38080

旧答案:有时有效

好的 --- 我简化了精彩的@marmot 回答......这是一个小包:

警告:它有时仍会爆炸(不知道为什么,没时间继续挖掘)。你绝对不应该嵌套tikzpgf拍照……

% save as marmotcsteps.sty
\NeedsTeXFormat{LaTeX2e}[1996/06/01]
\ProvidesPackage{marmotcsteps}[2019/06/19 Circled steps that you can reference and use in and outside tikz v1]
\RequirePackage{tikz}
% invented by @marmot: https://tex.stackexchange.com/a/496379/38080
% streamlined by @Rmano 

\pgfdeclareshape{stubborn ellipse}
% Draws a circle around the text
{%
  \savedanchor\centerpoint{%
    \pgf@x=.5\wd\pgfnodeparttextbox%
    \pgf@y=.5\ht\pgfnodeparttextbox%
    \advance\pgf@y by-.5\dp\pgfnodeparttextbox%
  }%
  \savedanchor\radius{%
    % Calculate ``height radius''
    \pgf@y=.5\ht\pgfnodeparttextbox
    \pgfmathsetlength\pgf@yb{\pgfkeysvalueof{/csteps/inner ysep}}%
    \advance\pgf@y by\pgf@yb%
    % Calculate ``width radius''
    \pgf@x=.5\wd\pgfnodeparttextbox%
    \pgfmathsetlength\pgf@xb{\pgfkeysvalueof{/csteps/inner xsep}}%
    \advance\pgf@x by\pgf@xb%
    % Adjust so that it's a circle for 1-digit numbers
    \ifdim\pgf@x<\pgf@y\pgf@x=\pgf@y\fi
  }%
  % Anchors
  \anchor{center}{\centerpoint}%
  % Background path
  \backgroundpath
  {%
    \pgf@process{\radius}%
    \pgfutil@tempdima=\pgf@x%
    \pgfutil@tempdimb=\pgf@y%
    \pgfpathellipse{\centerpoint}{\pgfqpoint{\pgfutil@tempdima}{0pt}}{\pgfqpoint{0pt}{\pgfutil@tempdimb}}%
  }%
}%
\newcommand*\marmotcircled[1]{\setbox0\hbox{#1}%
    \pgf@ya=\pgfkeysvalueof{/csteps/inner ysep}%
    \raisebox{-\pgf@ya}{%
        \begin{pgfpicture}\pgfsetcolor{\pgfkeysvalueof{/csteps/outer color}}%
            \pgfnode{stubborn ellipse}{center}{%
            \textcolor{\pgfkeysvalueof{/csteps/inner color}}{#1}}{}{\pgfusepath{stroke}}%
        \end{pgfpicture}%
    }}
\newcounter{cstepcnt}
\newcommand{\startcstep}{\setcounter{cstepcnt}{0}}
\newcommand{\cstep}{%
    \refstepcounter{cstepcnt}%
    \typeout{\number\value{cstepcnt}}%
    \marmotcircled{\number\value{cstepcnt}}% this is pretty dangerous
}
\renewcommand{\thecstepcnt}{\textbf{\arabic{cstepcnt}:}}
%
\pgfkeys{/csteps/inner ysep/.initial=1pt,
    /csteps/inner xsep/.initial=1pt,
    /csteps/inner color/.initial=red,
    /csteps/outer color/.initial=blue,
}
%

你可以使用它作为:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
%
\usepackage{marmotcsteps}

\begin{document}
one text here \cstep{} some text here \cstep\label{c:one}.

\begin{tikzpicture}
    \draw (0,0) node[draw, text width=5cm](A){text\cstep\label{c:inner}};
    \draw (4,0) node[draw, color=green](A){text\cstep\label{c:inner2}};
\end{tikzpicture}

And then \foreach \i in {0,...,12} {\cstep\ }

And then ref~\ref{c:one} and \ref{c:inner}.
\end{document}

给予:

在此处输入图片描述

相关内容