2

Consider the following .sty file:

% Den krævede LaTeX-version.
\NeedsTeXFormat{LaTeX2e}[2014/05/01]

% Pakkens navn.
\ProvidesPackage{standardopsaetning}[2014/10/16 standardopsaetning]


%%%%%%%%%%%%%%%%
%%            %%
%%   Pakker   %%
%%            %%
%%%%%%%%%%%%%%%%


% Sørger for dansk orddeling og dansk opsætning af `specielle' ord.
\RequirePackage{babel}

% Hjælper med orddeling ved `æ', `ø' og `å'.
\RequirePackage[T1]{fontenc}

% Tillader `æ', `ø' og `å' (i inputfilen).
\RequirePackage[utf8]{inputenc}

% Marginer med mere.
\RequirePackage[
  hmargin = 2.4cm,
  vmargin = 3cm
]{geometry}

% Matematik.
\RequirePackage{mathtools}

% Fodnoter.
\RequirePackage[
  bottom,
  hang,
  stable
]{footmisc}

% Sidehoved og -fod.
\RequirePackage{fancyhdr}

% Figur- og tabelforklaringer.
\RequirePackage{caption}

% SI-enheder.
\RequirePackage[
  locale = DE
]{siunitx}

% Intern vektorgrafik.
\RequirePackage{pstricks-add}

% Tabeller.
\RequirePackage{booktabs}

% Lister.
\RequirePackage{enumitem}

% Opstående græske bogstaver.
\RequirePackage{upgreek}

% Den sidste værdi, der angives af en tæller (`counter').
\RequirePackage{totcount}

% Mikrotypografiudvidelser til PDFTeX.
\RequirePackage{microtype}


%%%%%%%%%%%%%%%%%%%%%%%%
%%                    %%
%%   Pakkeopsætning   %%
%%                    %%
%%%%%%%%%%%%%%%%%%%%%%%%


\DeclareOption{pakkeopsaetninger}{
% ``mathtools''
\mathtoolsset{
  showonlyrefs
}
% ``caption''
\DeclareCaptionLabelSeparator{tilpasning}{:\quad}
\captionsetup{
  labelsep = tilpasning,
  figureposition = bottom,
  tableposition = top,
  font = small,
  labelfont = sc
}
% ``pstricks''
\psset{
  dimen = m
}
}


%%%%%%%%%%%%%%%%%
%%             %%
%%   Makroer   %%
%%             %%
%%%%%%%%%%%%%%%%%


\DeclareOption{makroer}{
% Til at afgøre, om dokumentet skal udskrives eller læses på en skærm.
\newif\ifprint
% Til sænket skrift.
\newif\ifsubscript
\subscriptfalse

% Figur- og tabelplaceringer.
\providecommand*\setfloatlocations[2]{\@namedef{fps@#1}{#2}}
\setfloatlocations{figure}{htbp}
\setfloatlocations{table}{htbp}

% Fjerner ligaturer ved `f'.
\DisableLigatures[f]{encoding = T1}

% Indryk til fodnoter.
\setlength\footnotemargin{6pt}

% Sidehoved og -fod.
\pagestyle{fancy}
\renewcommand*\headrulewidth{0pt}
\setlength\headheight{14.5pt}
\lhead{}
\rhead{}
\regtotcounter{page}
\cfoot{
  \ifnum \totvalue{page} > 1 \relax
    \thepage
  \else
%
  \fi
}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                         %%
%%   `Speciel' placering   %%
%%   af `\DeclareOption'   %%
%%                         %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Sænket skrift.
\begingroup\lccode`\~=`\_
\lowercase{\endgroup
  \DeclareOption{saenketSkrift}{%
    \protected\gdef~{\@ifnextchar|\subtextup\sb}%
    \def\subtextup|#1|{\sb{\textup{#1}}}%
    \AtBeginDocument{\catcode`\_=12 \mathcode`\_=\string"8000 }%
  }%
}

% Aritmetiske beregninger.
\ExplSyntaxOn
\DeclareOption{aritmetik}{%
  \cs_new_eq:NN \calc \fp_eval:n
}
\ExplSyntaxOff

% Addition eller subtraktion af fysiske størrelser med ``siunitx''.
\ExplSyntaxOn

\NewDocumentCommand\SIexpr{ O{,} m m }
 {
  \SIextension_siexpr:nnn { #1 } { #2 } { #3 }
 }

\cs_new_protected:Npn \SIextension_siexpr:nnn #1 #2 #3
 {
  \seq_set_split:Nnn \l_SIextension_siexpr_input_seq { #1 } { #2 }
  \seq_pop_left:NN \l_SIextension_siexpr_input_seq \l_SIextension_siexpr_first_tl
  \seq_clear:N \l_SIextension_siexpr_output_seq
  \seq_put_right:Nx \l_SIextension_siexpr_output_seq
   {
    \fp_compare:nTF { \l_SIextension_siexpr_first_tl > 0 }
      { \num { \l_SIextension_siexpr_first_tl } }
      { - \num { \tl_tail:V \l_SIextension_siexpr_first_tl } }
   }
  \seq_map_inline:Nn \l_SIextension_siexpr_input_seq
   {
    \seq_put_right:Nx \l_SIextension_siexpr_output_seq
     {
      \fp_compare:nTF { ##1 > 0 }
        { + \num { ##1 } }
        { - \num { \tl_tail:n { ##1 } } }
     }
   }

  \SI[
    parse-numbers = false,
    quotient-mode = fraction,
    input-product = *,
    output-product = \cdot
  ]
   {
    \sisetup{parse-numbers}
    \left( \seq_use:Nn \l_SIextension_siexpr_output_seq { } \right)
   }
   { #3 }
}
\ExplSyntaxOff


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                %%
%%   Eksekvering af opsætningen   %%
%%                                %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\ExecuteOptions{pakkeopsaetninger}
\ExecuteOptions{makroer}
\ExecuteOptions{saenketSkrift}
\ExecuteOptions{aritmetik}
\ProcessOptions

The subscript code (the chunk with \DeclareOption{saenketSkrift}) is no longer working (which it has in the past). Why is that and how do I make it work again?

P.S. The code in question is taken from here.

  • It's completely useless to add options that are executed no matter what. Just add the code (after \ProcessOptions). Anyway, the error is having \newif in the code for \DeclareOption. – egreg Oct 20 '14 at 13:19
  • @egreg Okay. As you might have guessed, I'm completely new to creating .sty files. If I can make you 'clean up' my file in an answer, I've gladly accept your answer. – Svend Tveskæg Oct 20 '14 at 13:22

1 Answers1

1

You define options that the user cannot override in any way: they're not option, then.

Here's a polished up version with just one option, that is, pakkeopsaetninger. The language is also passed as an option, but I wouldn't do it and make users load explicitly babel with the languages they need.

I removed the comments because I can't understand them; I also changed the totpages approach, that's not really good, preferring the zref-lastpage one.

\NeedsTeXFormat{LaTeX2e}[2014/05/01]
\ProvidesPackage{standardopsaetning}[2014/10/16 standardopsaetning]

\RequirePackage{mathtools}

\DeclareOption{pakkeopsaetninger}{\mathtoolsset{showonlyrefs}}
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{babel}}

\ProcessOptions\relax

\RequirePackage[T1]{fontenc}
\RequirePackage[utf8]{inputenc}
\RequirePackage{babel}

\RequirePackage[
  hmargin = 2.4cm,
  vmargin = 3cm
]{geometry}
\RequirePackage[
  bottom,
  hang,
  stable
]{footmisc}
\RequirePackage{fancyhdr}
\RequirePackage{caption}
\RequirePackage[
  locale = DE
]{siunitx}
\RequirePackage{pstricks-add}
\RequirePackage{booktabs}
\RequirePackage{enumitem}
\RequirePackage{upgreek}
\RequirePackage[lastpage]{zref}
\RequirePackage{microtype}

% ``caption''
\DeclareCaptionLabelSeparator{tilpasning}{:\quad}
\captionsetup{
  labelsep = tilpasning,
  figureposition = bottom,
  tableposition = top,
  font = small,
  labelfont = sc
}
% ``pstricks''
\psset{
  dimen = m
}


\newif\ifprint
\newif\ifsubscript

\providecommand*\setfloatlocations[2]{\@namedef{fps@#1}{#2}}
\setfloatlocations{figure}{htbp}
\setfloatlocations{table}{htbp}

\DisableLigatures[f]{encoding = T1}

\setlength\footnotemargin{6pt}

\pagestyle{fancy}
\renewcommand*\headrulewidth{0pt}
\setlength\headheight{14.5pt}

% http://tex.stackexchange.com/a/4258
\zref@newprop*{numpage}{\the\value{page}}
\zref@addprop{main}{numpage}

\fancyhf{}
\fancyfoot[C]{%
  \ifnum \zref@extractdefault{LastPage}{numpage}{0} > 1
    \thepage
  \fi
}


\begingroup\lccode`\~=`\_
\lowercase{\endgroup
  \protected\def~{\@ifnextchar|\subtextup\sb}%
}
\def\subtextup|#1|{\sb{\textup{#1}}}%
\AtBeginDocument{\catcode`\_=12 \mathcode`\_=\string"8000 }

\ExplSyntaxOn
\cs_new_eq:NN \calc \fp_eval:n

\NewDocumentCommand\SIexpr{ O{,} m m }
 {
  \SIextension_siexpr:nnn { #1 } { #2 } { #3 }
 }

\cs_new_protected:Npn \SIextension_siexpr:nnn #1 #2 #3
 {
  \seq_set_split:Nnn \l_SIextension_siexpr_input_seq { #1 } { #2 }
  \seq_pop_left:NN \l_SIextension_siexpr_input_seq \l_SIextension_siexpr_first_tl
  \seq_clear:N \l_SIextension_siexpr_output_seq
  \seq_put_right:Nx \l_SIextension_siexpr_output_seq
   {
    \fp_compare:nTF { \l_SIextension_siexpr_first_tl > 0 }
      { \num { \l_SIextension_siexpr_first_tl } }
      { - \num { \tl_tail:V \l_SIextension_siexpr_first_tl } }
   }
  \seq_map_inline:Nn \l_SIextension_siexpr_input_seq
   {
    \seq_put_right:Nx \l_SIextension_siexpr_output_seq
     {
      \fp_compare:nTF { ##1 > 0 }
        { + \num { ##1 } }
        { - \num { \tl_tail:n { ##1 } } }
     }
   }

  \SI[
    parse-numbers = false,
    quotient-mode = fraction,
    input-product = *,
    output-product = \cdot
  ]
   {
    \sisetup{parse-numbers}
    \left( \seq_use:Nn \l_SIextension_siexpr_output_seq { } \right)
   }
   { #3 }
}
\ExplSyntaxOff

\endinput

Here's a test document. Of course I can't test all the commands. Please test it and report any problem.

\documentclass{article}
\usepackage[danish]{standardopsaetning}

\usepackage{lipsum}

\begin{document}
\lipsum[1]
\end{document}
egreg
  • 1,121,712
  • Really nice. The only problem I can locate (for now) is that \mathtoolsset{showonlyrefs} doesn't seems to work. – Svend Tveskæg Oct 20 '14 at 14:08
  • @SvendTveskæg You need to pass the pakkeopsaetninger option to have it. I left it just to show an option that is really optional. – egreg Oct 20 '14 at 14:20
  • I see. Btw. what's the difference between \DeclareOption and \DeclareOption*? – Svend Tveskæg Oct 20 '14 at 14:24
  • 1
    @SvendTveskæg \DeclareOption* is for options not explicitly declared, typically passed to other packages; with texdoc clsguide you can read much more about package writing. – egreg Oct 20 '14 at 14:55
  • A follow-up question: Is it important where I load the fixltx2e package, in case I choose to do it? (I'm thinking of the loading-order of the packages.) – Svend Tveskæg Nov 14 '14 at 14:10
  • 1
    @SvendTveskæg Load fixltx2e as soon as possible. – egreg Nov 14 '14 at 15:31
  • 1
    @SvendTveskæg Yes, the first \RequirePackage after the start with \NeedsTeXFormat and \ProvidesPackage. – egreg Nov 14 '14 at 15:46