7

I would like to be able to detect if the standalone package was used with [preview=false] or [preview=true]. Not too concerned if this setting was a default or user defined.

Based on How can one undefine an already processed class option?, the macro @classoptionslist is supposed to be a comma separated list of options passed to the class. I attempted to detect if this macro had the string preview=true in it, but this does not yield the correct results. The following always prints Package Option: [preview=false].

\documentclass[preview=true]{standalone}% Does not work
%\documentclass[preview=false]{standalone}% works

\usepackage{xstring}
\makeatletter%
\newcommand*{\DetectPreviewOption}{%
    \IfSubStr{@classoptionslist}{preview=true}{true}{false}%
}%
\makeatother%

\begin{document}
Package Option: [preview=\DetectPreviewOption].
\end{document}
Peter Grill
  • 223,288

2 Answers2

7
\documentclass[preview]{standalone}
\makeatletter%
\newcommand*\DetectPreviewOption{%
  \ifsa@preview true\else false\fi}
\makeatother%

\begin{document}
Package Option: [preview=\DetectPreviewOption].
\end{document}
  • @Peter: to elaborate: the preview option of the standalone class is stored as the if-switch \ifsa@preview, so you can simply use this switch. – Martin Scharrer Sep 04 '11 at 21:11
  • It's a bit harder to undo the effect of preview=true: the preview package would be already loaded. – egreg Sep 04 '11 at 21:14
  • @Martin: as already done ,-) –  Sep 05 '11 at 06:01
  • @Herbert: Yes, I just wanted to state it explicitly. Some beginners might have issues reading it from the source code alone. – Martin Scharrer Sep 05 '11 at 06:39
  • @Martin: using directly the switch implies the makeatletter...\makeatother which is annoying ... Creating another name makes sense! –  Sep 05 '11 at 13:58
  • Sure, With "directly" I meant by using the if-switch (e.g. like you did) and to avoid any classoptionslist trickery as original suggested. I just wanted to add some text to your code. – Martin Scharrer Sep 05 '11 at 14:05
4

Normally, you would be right and \@classoptionlist would have this information in (try your example using the article class). The behaviour you are seeing is a 'feature' of the standalone class, which includes the lines

\def\sa@classoptionslist{}
\DeclareDefaultOption{%
  \xdef\sa@classoptionslist{\sa@classoptionslist,\CurrentOption}%
}
\ProcessKeyvalOptions*\relax
\let\@classoptionslist\sa@classoptionslist

which will remove all of the known options from \@classoptionslist.

I see that Herbert has proposed a solution: I'm answering here to point out why the approach you've tried is not working in this case.

lockstep
  • 250,273
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • \sa@classoptionslist loads only undeclared options (\DeclareDefaultOption); \@classoptionslist is made equal to it so the undeclared options are passed to the packages as usual. The declared options, among which is the boolean preview, are dealt with \ProcessKeyvalOptions (and are not passed to packages, I guess). – egreg Sep 04 '11 at 21:10
  • @egreg: That's what I meant by 'remove the known options', as it copies the unknown ones only 'over' the full list. – Joseph Wright Sep 04 '11 at 21:16
  • How to print all options used within \documentclass[]{}? – Sigur Nov 11 '18 at 00:37