有条件显示的文本:有没有办法以编程方式隐藏文本?

有条件显示的文本:有没有办法以编程方式隐藏文本?

我希望能够声明

\node[somestyle] (mynode) {\somekindoftext BILLY'S MAGIC CLOSET!!!};

我可以做

\def\somekindoftext{}

正常显示文本,或

\def\somekindoftext{... what goes here ...}

完全隐藏文本,并防止其在输出文件(PDF 然后可以转换为 SVG)中呈现。

有办法吗?透明似乎是一种选择,但我想防止它出现在 PDF 或 SVG 中。

我想占据空间。\phantom似乎也很接近,但我必须在以下内容周围添加括号。

答案1

下面定义了一个宏,如果使用了该宏\magictrick,它将使直到下一个匹配的所有内容都变得}不可见( ) ,否则什么也不做。它可能不是万能的,尤其是尽管它似乎在以下 MWE 中起作用,但我不能保证它总是能起作用,也不能保证它在任意代码上都能很好地运行,我已经警告过你了。\phantom\activatemagic

\documentclass{article}

% required for magictrick
\usepackage{pgfparser}
\makeatletter
\newcount\magictrick@level
\protected\def\magictrick@add#1%
  {\edef\magictrick@contents{\unexpanded\expandafter{\magictrick@contents#1}}}
\protected\def\magictrick@init
  {%
    \let\magictrick@contents\@empty
    \magictrick@level=\z@
  }
\protected\def\magictrick@finalise
  {\edef\magictrick@contents{\magictrick@contents}}
\pgfparserdef{magictrick}{all}{\meaning\bgroup}
  {%
    \advance\magictrick@level\@ne
    \magictrick@add{{\iffalse}\fi}%
  }
\pgfparserdef{magictrick}{all}{blank space}{\magictrick@add{ }}
\pgfparserdef{magictrick}{all}{\meaning\egroup}
  {%
    \advance\magictrick@level\m@ne
    \ifnum\magictrick@level<\z@
      \pgfparserswitch{final}%
    \else
      \magictrick@add{\iffalse{\fi}}%
    \fi
  }
\pgfparserdefunknown{magictrick}{all}
{\expandafter\magictrick@add\expandafter{\expandafter\unexpanded\expandafter{\pgfparserletter}}}
\pgfparserdeffinal{magictrick}
  {%
    \magictrick@finalise
    \expanded
      {%
        \endgroup
        \noexpand\phantom{\unexpanded\expandafter{\magictrick@contents}}%
        \iffalse{\fi}%
      }%
  }
\protected\def\magictrick@active
  {%
    \begingroup
    \magictrick@init
    \pgfparserparse{magictrick}%
  }
\@ifdefinable{\activatemagic}
  {\protected\def\activatemagic{\let\magictrick\magictrick@active}}
\@ifdefinable{\deactivatemagic}
  {\protected\def\deactivatemagic{\let\magictrick\@empty}}
\newcommand*\magictrick{}
\pgfparserset{magictrick/silent}
\makeatother

\usepackage{tikz}

\begin{document}
A{\magictrick Billy's Magic Closet!!!}A

\activatemagic
A{\magictrick Billy's Magic Closet!!!}A

\begin{tikzpicture}
  \node[draw] (mynode) {\magictrick BILLY'S MAGIC CLOSET!!!};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我不确定您想要实现什么,或者我是否正确理解了您的意思,但也许这样的事情对您有用:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\newif\ifsomekindoftextvisible
\newcommand{\somekindoftext}{}
\newcommand{\setsomekindoftext}[1]{%
    \renewcommand{\somekindoftext}{%
        \ifsomekindoftextvisible #1%
        \else\phantom{#1}%
        \fi%
    }%
}

\begin{document}
\begin{tikzpicture}[somestyle/.style={}]

\setsomekindoftext{Hello }

\somekindoftextvisibletrue
\node[somestyle] at (0,0) (mynodeA) {\somekindoftext BILLY'S MAGIC CLOSET!!!};

\somekindoftextvisiblefalse
\node[somestyle] at (0,-1) (mynodeB) {\somekindoftext BILLY'S MAGIC CLOSET!!!};

\somekindoftextvisibletrue
\node[somestyle] at (0,-2) (mynodeC) {\somekindoftext BILLY'S MAGIC CLOSET!!!};

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容