2

Trying to build expandable macros using ocg layers, I am facing quite disturbing issue that I am not able to figure out and so turn here for some help of specialists...

I am using a macro to insert some tikzpicture content. The visibility of this content is controlled using ocmd from the ocgx2 package. In the main file I want to declare once layers from which the object may stay hidden. For that purpose I created a list of layers using expl3 in order to use it in the macro.

Problem: As far as the list of layers is given by hand everything works well, however as soon as I am passing the list through the expl3 list (and keyval value) it breaks.

Here is a minimal exemple, from which I really need to keep the structure:

\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}

The output is the following:

enter image description here

whereas the circle may not be displayed! I am very new in using ocgx and expl3 and would be gratefull for any help.

PS: I am so curious of what you can do further with ocgx. Any other exemple would be very cool.

1

Yet another possibility: The three new commands \nxAnd, \nxOr, \nxNot allow us to define TikZ styles for visibility setting that are based on arbitrary boolean expressions as in

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

Full example:

\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}
1

You are trying very hard to hide your values behind different layers of (in part not expandable) commands. If one expand everything it works:

\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}
2
  • Also possible (Line 13): \begingroup\edef\DOIT{\endgroup\noexpand\tikzstyle{HCocmd}=[ocmd={visibility=\noexpand\Not{\noexpand\Or{\hidencircle@trigger}}}]}\DOIT %<-- – AlexG Jun 21 '20 at 20:53
  • Thanks a lot for the help. That works really well. The example might look quite artificial but for my purpose it allows me now to easily control appearance of different types of "objects" defined in a header file with complex rules without having to change everything as I include new layer-tabs. I attempted \expandafter everywhere but didn't know about \NewExpandableDocumentCommand. Nevertheless their might be a better way of doing this... Thank you very much AlexG for ocgx2 – Damien Gredat Jun 22 '20 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.