ocg:使用 keyval 时出现问题

ocg:使用 keyval 时出现问题

尝试使用 ocg 层构建可扩展宏,我面临着相当令人不安的问题,我无法弄清楚,因此来这里寻求专家的帮助......

我正在使用宏插入一些 tikzpicture 内容。此内容的可见性由以下方式控制:奥克姆来自ocgx2包。在主文件中,我想声明一次对象可能隐藏的层。为此,我使用创建了一个层列表解释3以便在宏中使用它。

问题:只要手动给出层列表,一切都运行良好,但是,只要我将列表传递给 expl3 列表(和 keyval 值),它就会中断。

这是一个最小的例子,我确实需要保留其结构:

\documentclass{article}
\usepackage[tikz]{ocgx2}
\usepackage{keyval}

\makeatletter

\define@key{hidencircle}{trigger}{\def\hidencircle@trigger{#1}}

\newcommand{\hidencircle}[2][]{%
\setkeys{hidencircle}{#1}% Set the keys
    
\begin{tikzpicture}[overlay,shift={(2.75,1)}]
%%%%%%%%%%%%%% Not Working
\tikzstyle{HCocmd}=[ocmd={visibility=\Not{\Or{\hidencircle@trigger}}}]
%%%%%%%%%%%%%% Does Work
%\tikzstyle{HCocmd}=[ocmd={visibility=\Not{\Or{test1,test2}}}]

\begin{scope}[HCocmd]
\node[circle,fill=red,text=white,text width=1cm]{\bf #2};
\end{scope}
\end{tikzpicture}
}
\makeatother

\ExplSyntaxOn

\NewDocumentCommand{\createlist}{mm}
{\seq_new:c { g_#1_seq }
\seq_gset_from_clist:cn { g_#1_seq } { #2 }}

\NewDocumentCommand{\ocglist}{m}
{\seq_use:cn {g_#1_seq} {,}}

\ExplSyntaxOff


\begin{document}

\createlist{trig}{test1,test2}

\hidencircle[trigger=\ocglist{trig}]{Here I am!}

\tikz{
\begin{scope}[ocg={ref=test1,status=on}]
\node[red](I1){\small circle set invisible};
\end{scope}
\begin{scope}[ocg={ref=test2,status=on}]
    \node[blue,anchor=west](I2)at(I1.east){\small circle set invisible};
\end{scope}

\node[switch ocg={test1},fill=green,text=red] (I3)at([yshift=-.5cm]I1.south) {show it: step 1};
\node[switch ocg={test2},fill=green,anchor=west,text=blue] at([xshift=.25cm]I3.east) {show it: step 2};
}


\end{document}

输出如下:

在此处输入图片描述

而圆圈可能无法显示!我是 ocgx 和 expl3 的新手,如能得到任何帮助我将不胜感激。

附言:我很好奇你能用 ocgx 做什么。如果有其他例子就更好了。

答案1

还有一种可能性:三个新命令\nxAnd\nxOr\nxNot允许我们定义基于任意布尔表达式的 TikZ 可见性设置样式,如下所示

\defVisibilityStyle{HCocmd}{\nxNot{\nxOr{\hidencircle@trigger}}}

完整示例:

\documentclass{article}
\usepackage[tikz]{ocgx2}
\usepackage{keyval}

\makeatletter

\define@key{hidencircle}{trigger}{\def\hidencircle@trigger{#1}}%

\newcommand{\hidencircle}[2][]{%
\setkeys{hidencircle}{#1}% Set the keys

\begin{tikzpicture}[overlay,shift={(2.75,1)}]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\defVisibilityStyle{HCocmd}{\nxNot{\nxOr{\hidencircle@trigger}}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{scope}[HCocmd]
\node[circle,fill=red,text=white,text width=1cm]{\bf #2};
\end{scope}
\end{tikzpicture}
}
\makeatother

\ExplSyntaxOn

\NewDocumentCommand{\createlist}{mm}
{\seq_new:c { g_#1_seq }
\seq_gset_from_clist:cn { g_#1_seq } { #2 }}

\NewExpandableDocumentCommand{\ocglist}{m} %<---
{\seq_use:cn {g_#1_seq} {,}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\cs_new_nopar:Nn\defVisibilityStyle:nn{ \tikzstyle{#1}=[ocmd={visibility=#2}] }
\cs_generate_variant:Nn\defVisibilityStyle:nn{nx}
\cs_set_eq:NN\defVisibilityStyle\defVisibilityStyle:nx
\cs_new_protected_nopar:Npn\nxNot{\Not}
\cs_new_protected_nopar:Npn\nxAnd{\And}
\cs_new_protected_nopar:Npn\nxOr{\Or}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ExplSyntaxOff

\begin{document}

\createlist{trig}{test1,test2}

\hidencircle[trigger=\ocglist{trig}]{Here I am!}

\tikz{
\begin{scope}[ocg={ref=test1,status=on}]
\node[red](I1){\small circle set invisible};
\end{scope}
\begin{scope}[ocg={ref=test2,status=on}]
    \node[blue,anchor=west](I2)at(I1.east){\small circle set invisible};
\end{scope}

\node[switch ocg={test1},fill=green,text=red] (I3)at([yshift=-.5cm]I1.south) {show it: step 1};
\node[switch ocg={test2},fill=green,anchor=west,text=blue] at([xshift=.25cm]I3.east) {show it: step 2};
}

\end{document}

答案2

您正在努力将您的值隐藏在不同层(部分不可扩展)的命令后面。如果展开所有内容,它就会起作用:

\documentclass{article}
\usepackage[tikz]{ocgx2}
\usepackage{keyval}

\makeatletter

\define@key{hidencircle}{trigger}{\edef\hidencircle@trigger{#1}}%<-- edef

\newcommand{\hidencircle}[2][]{%
\setkeys{hidencircle}{#1}% Set the keys

\begin{tikzpicture}[overlay,shift={(2.75,1)}]
\tikzstyle{HCocmd}=[ocmd={visibility=\expandafter\Not\expandafter{\expandafter\Or\expandafter{\hidencircle@trigger}}}] %<--
%%%%%%%%%%%%%% Does Work
%\tikzstyle{HCocmd}=[ocmd={visibility=\Not{\Or{test1,test2}}}]

\begin{scope}[HCocmd]
\node[circle,fill=red,text=white,text width=1cm]{\bf #2};
\end{scope}
\end{tikzpicture}
}
\makeatother

\ExplSyntaxOn

\NewDocumentCommand{\createlist}{mm}
{\seq_new:c { g_#1_seq }
\seq_gset_from_clist:cn { g_#1_seq } { #2 }}

\NewExpandableDocumentCommand{\ocglist}{m} %<---
{\seq_use:cn {g_#1_seq} {,}}

\ExplSyntaxOff


\begin{document}

\createlist{trig}{test1,test2}

\hidencircle[trigger=\ocglist{trig}]{Here I am!}

\tikz{
\begin{scope}[ocg={ref=test1,status=on}]
\node[red](I1){\small circle set invisible};
\end{scope}
\begin{scope}[ocg={ref=test2,status=on}]
    \node[blue,anchor=west](I2)at(I1.east){\small circle set invisible};
\end{scope}

\node[switch ocg={test1},fill=green,text=red] (I3)at([yshift=-.5cm]I1.south) {show it: step 1};
\node[switch ocg={test2},fill=green,anchor=west,text=blue] at([xshift=.25cm]I3.east) {show it: step 2};
}


\end{document}

相关内容