GeneralIs it possible to read a boolean value from a parent pkg?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
cypherpunks
Posts: 15
Joined: Sat Dec 10, 2016 12:26 pm

Is it possible to read a boolean value from a parent pkg?

Post by cypherpunks »

It would sometimes be useful to write conditional code that depends on boolean values held by a parent package. E.g. the \pdfcomment package has the boolean “final”, which disables all PDF annotations in the document. There is some other logic in the document that should also be disabled when that boolean is true. I tried simply using:

Code: Select all

\ifpc@gopt@final\else%
…code that should not run when final is true…
\fi
pdflatex gives: “Undefined control sequence”

More generally, many draft options are often useful for controlling logic within the document for which a parent uses a draft option. Also when defining a custom letterhead in the scrlttr2 class there are booleans for many items that may or may not be wanted in the letterhead.

(update) This thread gives useful options for many situations. But it does not completely answer the question because there are non-draft related booleans. And setting the draft boolean at the document class level can have widespread unwanted effects.
Last edited by cypherpunks on Sat Jan 11, 2025 11:19 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Re: Is it possible to read a boolean value from a parent pkg?

Post by Stefan Kottwitz »

Hi,

that code works fine for me as expected when I use it.

Since the code is incomplete, we cannot know what you are doing, like not loading the pdfcomment package of something else. Perhaps post a Infominimal working example.

Stefan
LaTeX.org admin
cypherpunks
Posts: 15
Joined: Sat Dec 10, 2016 12:26 pm

Re: Is it possible to read a boolean value from a parent pkg?

Post by cypherpunks »

Glad to hear you were able to use it. That gives me hope.

This code sample shows “\ifpc@gopt@final\else” causing an “undefined control sequence”:

Code: Select all

\documentclass{article}

% Demonstrates populating a PDF annotation with text input from an external file.
%
% Bugs/Limitations/anti-features:
% * (serious) Percent symbols in the input text are lost entirely, along with all text on to the right of a % on that line!
% * Line breaks are lost. (this is a feature for some situations; use a blank line in the source text to force a line break)
% * Every blank line in the source triggers an extra space in the PDF comment.
% * (w.i.p) Sanitization is limited to “_”, “&”, “%” (lossy), and blank lines. There are likely many more strings that need to be controlled.
% * Some file i/o is likely unnecessary. Intermediate text is staged in \jobname_filecycle.txt.

\usepackage[final]{pdfcomment}
\usepackage{newfile}     % furnishes \newoutputstream et al.
\usepackage{catchfile}   % furnishes \CatchFileDef
\usepackage{xstring}     % furnishes \StrSubstitute

\begin{filecontents*}[overwrite]{\jobname_sample.txt}
line one

line two
% pre-tricky line (lost completely because of the leading percent symbol!)
tricky symbols: _&%
\end{filecontents*}

% Furnish a \pdfcommentfile command which takes a filename as input and feeds the contents of the file to \pdfcomment:
\makeatletter
\gdef\pdfcommentfile#1{%
  \begingroup
  \everyeof{\noexpand}%
  \long\edef\temp{\noexpand\pdfcomment{\@@input{#1}}}%
  \temp
  \endgroup
}%
\makeatother

\newcommand{\safepdfcomment}[1]{
  \ifpc@gopt@final\else%
  \CatchFileDef{\unsafetext}{#1}{} % side-effects: replaces blank lines with “\par”, loses linebreaks, and drops percent symbols+all text to the right of %

  \message{The pre-sanitized PDF annotation text is: \unsafetext}
  
  %\StrSubstitute{\unsafetext}{\par}{\string\noexpand\string\textLF }[\safertext] % sadly, the normal space after textLF breaks compilation
  \StrSubstitute{\unsafetext}{\par}{\string\noexpand\string\textLF\string\noexpand\string\textLF\ }[\safertext] % ← so a hard space is needed instead (bummer!).
  \StrSubstitute{\safertext}{\%}{\string\noexpand\string\%}[\safertext] % not sure if this line has effect
  \StrSubstitute{\safertext}{_}{\string\noexpand\string\_}[\safertext]
  \StrSubstitute{\safertext}{&}{\string\noexpand\string\&}[\safertext]

  \message{The sanitized PDF annotation text is: \safertext}
  
  % \pdfcomment{\safertext} % ← Sorry to say we cannot simply do this because it strangely causes Mandarin comments regardless of source language!

  \newoutputstream{filecycle}
  \openoutputfile{\jobname_filecycle.txt}{filecycle}
  \addtostream{filecycle}{\safertext}
  \closeoutputstream{filecycle}
  
  \pdfcommentfile{\jobname_filecycle.txt}
  \fi%
}

\begin{document}
Lorem ipsum…\\[2\baselineskip]

\safepdfcomment{\jobname_sample.txt}\\[2\baselineskip]

yada yada
\end{document}
If I remove the IF conditional, it compiles. But the file i/o that the conditional affects can really slow down the compiler, so the IF statement is needed.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Re: Is it possible to read a boolean value from a parent pkg?

Post by Stefan Kottwitz »

This runs fine:

Code: Select all

\documentclass{article}
\usepackage[final]{pdfcomment}
\begin{document}
\makeatletter
\ifpc@gopt@final\else%
...code that should not run when final is true…
\fi
Didn't run.
\end{document}
Just \makeatletter ... \makeatother is missing at the right place. You have it at one, but need it also around the \safepdfcomment definition. Very good minimal example, it shows the error and works when I add \makeatletter before the \safepdfcomment definition. There'a another error regarding the \\ linebreak, but that's another thing.

Stefan
LaTeX.org admin
cypherpunks
Posts: 15
Joined: Sat Dec 10, 2016 12:26 pm

Re: Is it possible to read a boolean value from a parent pkg?

Post by cypherpunks »

Ah, thanks! That fixed it in my MWE.

For some reason the same approach did not fix it in my large complex document. I’ll have to see if I can narrow that down. In the failing case, \safepdfcomment is used inside an \includepdf option, like this:

Code: Select all

\includepdf[pages=1,unit=1mm,pagecommand={%
  \safepdfcomment{english_version.txt}%$
}]{non_english.pdf}%$
And it only fails if final is false. I’ll see I can make an MWE for that.

UPDATE: nevermind -- I had a residual \ifdraft in my larger doc that was causing problems. It all works fine. Thanks for the help!
Last edited by cypherpunks on Sat Jan 11, 2025 11:48 pm, edited 1 time in total.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Re: Is it possible to read a boolean value from a parent pkg?

Post by Stefan Kottwitz »

Perhaps you need \makeatletter ... \makeatother within the \safepdfcomment definition instead of around.

Stefan
LaTeX.org admin
cypherpunks
Posts: 15
Joined: Sat Dec 10, 2016 12:26 pm

Re: Is it possible to read a boolean value from a parent pkg?

Post by cypherpunks »

Indeed I tried nesting it inside the command to reduce the scope of it, and actually /that/ caused problems. So the \makeatletter must surround the command definition. Once I removed an \ifdraft statement I forgot about, it was all good.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Re: Is it possible to read a boolean value from a parent pkg?

Post by Stefan Kottwitz »

Sounds good!
LaTeX.org admin
Post Reply