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}
6
  • 1
    And the test \newcommand*{\DetectPreviewOption}{% \ifsa@preview\relax true\else false\fi% }% isn't the right one? Commented Sep 4, 2011 at 21:07
  • @Marco: Yep that is the correct one, seems to work great. Commented Sep 4, 2011 at 21:14
  • Could it be that you were just missing the "backslash" character right before @classoptionslist in the 7th line of your example, that should then be: \IfSubStr{\@classoptionslist}{preview=true}{true}{false}%?
    – Olivier
    Commented Sep 4 at 10:42
  • @Olivier: Results are identical with/without the backslash in @classoptionslist. Commented Sep 4 at 20:50
  • 1
    @Olivier: No problem. But, you are correct, it should include the leading backslash, so should be \@classoptionslist (not @classoptionslist). Commented Sep 5 at 23:02

2 Answers 2

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

\begin{document}
Package Option: [preview=\DetectPreviewOption].
\end{document}
6
  • @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. Commented Sep 4, 2011 at 21:11
  • It's a bit harder to undo the effect of preview=true: the preview package would be already loaded.
    – egreg
    Commented Sep 4, 2011 at 21:14
  • @Martin: as already done ,-)
    – user2478
    Commented Sep 5, 2011 at 6:01
  • 1
    @Herbert: Yes, I just wanted to state it explicitly. Some beginners might have issues reading it from the source code alone. Commented Sep 5, 2011 at 6:39
  • @Martin: using directly the switch implies the makeatletter...\makeatother which is annoying ... Creating another name makes sense!
    – user2478
    Commented Sep 5, 2011 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.

4
  • \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
    Commented Sep 4, 2011 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
    Commented Sep 4, 2011 at 21:16
  • How to print all options used within \documentclass[]{}?
    – Sigur
    Commented Nov 11, 2018 at 0:37
  • @Sigur I know it's a bit late but you can just type \makeatletter\@classoptionslist\makeatother without any blank space where you want them to be printed out.
    – Olivier
    Commented Sep 4 at 10:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .