6

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}
  • 1
    And the test \newcommand*{\DetectPreviewOption}{% \ifsa@preview\relax true\else false\fi% }% isn't the right one? – Marco Daniel Sep 4 '11 at 21:07
  • @Marco: Yep that is the correct one, seems to work great. – Peter Grill Sep 4 '11 at 21:14
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 4 '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 4 '11 at 21:14
  • @Martin: as already done ,-) – user2478 Sep 5 '11 at 6: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 5 '11 at 6:39
  • @Martin: using directly the switch implies the makeatletter...\makeatother which is annoying ... Creating another name makes sense! – user2478 Sep 5 '11 at 13:58
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.

  • \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 4 '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 4 '11 at 21:16
  • How to print all options used within \documentclass[]{}? – Sigur Nov 11 '18 at 0:37

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.