2

I tried to make a document class out of a LaTeX document (that was generated from the default Pandoc LaTeX template, just FYI). The document compiled perfectly with XeLaTeX but I can't get the document class to work.

Here's jku.cls:

\NeedsTeXFormat{LaTeX2e}[2018/04/01]
\ProvidesClass{jku}[2019/01/03 JKU Template]

\RequirePackage{kvoptions}

\DeclareVoidOption{graphics}{
  \RequirePackage{graphicx,grffile}
  \graphicspath{{include/}{figures/}{images/}{../include/}{../figures/}{../images/}}
  \def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
  \def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
  % Scale images if necessary, so that they will not overflow the page
  % margins by default, and it is still possible to overwrite the defaults
  % using explicit options in \includegraphics[width, height, ...]{}
  \setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
}

\DeclareStringOption[english]{lang}
\DeclareStringOption{margin}
\DeclareStringOption{parspacing}[6pt]
\DeclareStringOption[6pt]{parskip}
\DeclareStringOption{linespacing}[1.5]
\DeclareStringOption[12pt]{fontsize}

\DeclareStringOption{lefthead}
\DeclareStringOption{righthead}
\DeclareStringOption{leftfoot}
\DeclareStringOption[\today]{rightfoot}[]

\DeclareBoolOption{tightlists}

\DeclareDefaultOption{
  \PassOptionsToClass{\CurrentOption}{article}
}

\ExecuteOptions{a4paper,12pt}
\ProcessKeyvalOptions*
\LoadClass{article}

\RequirePackage{lmodern,etoolbox,letltxmacro}
\RequirePackage{setspace}
\RequirePackage{amssymb,amsmath,siunitx}
\RequirePackage{ifxetex,ifluatex}
\RequirePackage{fancyhdr,datetime2}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \RequirePackage[T1]{fontenc}
  \RequirePackage[utf8]{inputenc}
\else % if luatex or xelatex
  \ifxetex
    \RequirePackage{mathspec}
  \else
    \RequirePackage{fontspec}
  \fi
  \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\RequirePackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\RequirePackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\notblank{\jku@margin}{
  \RequirePackage[margin=\jku@margin,includehead,includefoot]{geometry}
}{}
\RequirePackage[unicode=true]{hyperref}
\hypersetup{
  pdfborder={0 0 0},
  breaklinks=true
}
\urlstyle{same}  % don't use monospace font for urls
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \RequirePackage[shorthands=off,main=\jku@lang]{babel}
\else
  \RequirePackage{polyglossia}
  \setmainlanguage[]{\jku@lang}
\fi

\def\fps@figure{!htp}

\setlength{\emergencystretch}{3em}  % prevent overfull lines
\setcounter{secnumdepth}{0}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi

\LetLtxMacro{\@section}{\section}
\renewcommand{\section}[2][]{
  \hypertarget{#1}{\@section{#2}\label{sec:#1}}
}
\LetLtxMacro{\@subsection}{\subsection}
\renewcommand{\subsection}[2][]{
  \hypertarget{#1}{\@subsection{#2}\label{subsec:#1}}
}
\LetLtxMacro{\@subsubsection}{\subsubsection}
\renewcommand{\subsubsection}[2][]{
  \hypertarget{#1}{\@subsubsection{#2}\label{subsec:#1}}
}

\notempty{\jku@lefthead}{
  \fancyhead[L]{\jku@lefthead}
}{}
\notempty{\jku@righthead}{
  \fancyhead[R]{\jku@righthead}
}{}
\notempty{\jku@leftfoot}{
  \fancyfoot[L]{JKU/\jku@leftfoot}
}{}
\notempty{\jku@rightfoot}{
  \fancyfoot[R]{\jku@rightfoot}
}{}
\AtBeginDocument{\pagestyle{fancy}}

\ifjku@tightlists
  \AtBeginEnvironment{itemize}{
    \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}
  }
\fi

\notblank{\jku@parspacing}{
  \IfFileExists{parskip.sty}{%
    \RequirePackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{\jku@parspacing plus 2pt minus 1pt}
  }
}{}

\notblank{\jku@linespacing}{
  \AtBeginDocument{\setstretch{\jku@linespacing}}
}{}

\endinput

A usage example:

\documentclass[
lang=english,
margin=2.5cm,
lefthead=Example,
leftfoot=Lorem,
rightfoot=Ipsum,
graphics,
linespacing,
parspacing,
tightlists
]{jku}

\begin{document}

...

Here are the errors I'm getting:

jku.cls:36: Package keyval Error: linespacing undefined. [\ProcessKeyvalOptions*]
jku.cls:36: Package keyval Error: parspacing undefined. [\ProcessKeyvalOptions*]
jku.cls:36: Package keyval Error: tightlists undefined. [\ProcessKeyvalOptions*]
/usr/local/texlive/2018basic/texmf-dist/tex/latex/translator/translator.sty:93: LaTeX Error: Missing \begin{document}. [\ProcessOptions*]
/usr/local/texlive/2018basic/texmf-dist/tex/latex/translator/translator.sty:93: You can't use `macro parameter character #' in horizontal mode. [\ProcessOptions*]
/usr/local/texlive/2018basic/texmf-dist/tex/latex/translator/translator.sty:94: Paragraph ended before \reserved@a was complete. []
jku.cls:43: LaTeX Error: \RequirePackage or \LoadClass in Options Section. [\RequirePackage]
jku.cls:44: LaTeX Error: \RequirePackage or \LoadClass in Options Section. [\ifnum]
jku.cls:50: LaTeX Error: \RequirePackage or \LoadClass in Options Section. [  \else]
jku.cls:53: Undefined control sequence. [  \defaultfontfeatures]
jku.cls:53: LaTeX Error: Missing \begin{document}. [  \defaultfontfeatures{L]
jku.cls:58: LaTeX Error: \RequirePackage or \LoadClass in Options Section. [\IfFileExists]
jku.cls:61: LaTeX Error: \RequirePackage or \LoadClass in Options Section. [}{}]
jku.cls:61: Undefined control sequence. [}{}]
jku.cls:65: LaTeX Error: \RequirePackage or \LoadClass in Options Section. [\RequirePackage]
jku.cls:66: LaTeX Error: \RequirePackage or \LoadClass in Options Section. [\hypersetup]
jku.cls:66: Undefined control sequence. [\hypersetup]
jku.cls:70: Undefined control sequence. [\urlstyle]
jku.cls:75: LaTeX Error: \RequirePackage or \LoadClass in Options Section. [  \setmainlanguage]
jku.cls:75: Undefined control sequence. [  \setmainlanguage]
jku.cls:105: Undefined control sequence. [\notempty]
jku.cls:105: LaTeX Error: Missing \begin{document}. [\notempty{\jku@lefthead]
jku.cls:106: Undefined control sequence. [  \fancyhead]
jku.cls:108: Undefined control sequence. [\notempty]
jku.cls:109: Undefined control sequence. [  \fancyhead]
jku.cls:111: Undefined control sequence. [\notempty]
jku.cls:112: Undefined control sequence. [  \fancyfoot]
jku.cls:114: Undefined control sequence. [\notempty]
jku.cls:115: Undefined control sequence. [  \fancyfoot]
jku.cls:133: LaTeX Error: \RequirePackage or \LoadClass in Options Section. []
test.tex:13: Undefined control sequence. [\begin{document}]

Can someone point me in the right direction? I'm sure this is some kind of rookie mistake.

  • 2
    \RequirePackage{graphicx,....} cannot be applied in option code, I think. Correcting this, there is a \notempty error -- I don't know a command named \notempty... – user31729 Jan 3 at 0:56
  • Thanks, that must've been the cause. I confused \notempty with \notblank, so I replaced it with \ifstrempty. The strangest thing, though, is that all these errors disappeared after slightly changing the document file. Maybe some caching issue, or I was looking at an old log file… – vbwx Jan 3 at 18:43
  • @ChristianHupfer If you'd like you can add your comment as an answer so I can mark the question as solved. – vbwx Jan 3 at 18:52
  • I've added the comment as an answer and a by-pass solution – user31729 Jan 3 at 19:42
2

Package loading can not occur within a class or package option setting -- this would interrupt the evaluation of options.

As such, the graphics options causes the error, since it tries to apply \RequirePackage{graphicx,...}

Using an 'boolean' \ifgraphicsoption macro, the information that graphics option has been used can be stored and later checked on with \ifgraphicsoption...\fi the package may be loaded.

By definition, a \newif\iffoo will be initially always set to false.

I did not address other potential issues in the .cls file.

\NeedsTeXFormat{LaTeX2e}[2018/04/01]
\ProvidesClass{jku}[2019/01/03 JKU Template]

\RequirePackage{kvoptions}


\DeclareStringOption[english]{lang}
\DeclareStringOption{margin}
\DeclareStringOption{parspacing}[6pt]
\DeclareStringOption[6pt]{parskip}
\DeclareStringOption{linespacing}[1.5]
\DeclareStringOption[12pt]{fontsize}

\DeclareStringOption{lefthead}
\DeclareStringOption{righthead}
\DeclareStringOption{leftfoot}
\DeclareStringOption[\today]{rightfoot}[]

\DeclareBoolOption{tightlists}

\newif\ifgraphicsoption
\DeclareVoidOption{graphics}{
  \graphicsoptiontrue
}



\DeclareDefaultOption{
  \PassOptionsToClass{\CurrentOption}{article}
}


\ExecuteOptions{a4paper,12pt}
\ProcessKeyvalOptions*
\LoadClass{article}

\ifgraphicsoption
\RequirePackage{graphicx,grffile}
\graphicspath{{include/}{figures/}{images/}{../include/}{../figures/}{../images/}}
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
\fi

\RequirePackage{lmodern,etoolbox,letltxmacro}
\RequirePackage{setspace}
\RequirePackage{amssymb,amsmath,siunitx}
\RequirePackage{ifxetex,ifluatex}
\RequirePackage{fancyhdr,datetime2}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \RequirePackage[T1]{fontenc}
  \RequirePackage[utf8]{inputenc}
\else % if luatex or xelatex
  \ifxetex
    \RequirePackage{mathspec}
  \else
    \RequirePackage{fontspec}
  \fi
  \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\RequirePackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\RequirePackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\notblank{\jku@margin}{
  \RequirePackage[margin=\jku@margin,includehead,includefoot]{geometry}
}{}
\RequirePackage[unicode=true]{hyperref}
\hypersetup{
  pdfborder={0 0 0},
  breaklinks=true
}
\urlstyle{same}  % don't use monospace font for urls
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \RequirePackage[shorthands=off,main=\jku@lang]{babel}
\else
  \RequirePackage{polyglossia}
  \setmainlanguage[]{\jku@lang}
\fi

\def\fps@figure{!htp}

\setlength{\emergencystretch}{3em}  % prevent overfull lines
\setcounter{secnumdepth}{0}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi

\LetLtxMacro{\@section}{\section}
\renewcommand{\section}[2][]{
  \hypertarget{#1}{\@section{#2}\label{sec:#1}}
}
\LetLtxMacro{\@subsection}{\subsection}
\renewcommand{\subsection}[2][]{
  \hypertarget{#1}{\@subsection{#2}\label{subsec:#1}}
}
\LetLtxMacro{\@subsubsection}{\subsubsection}
\renewcommand{\subsubsection}[2][]{
  \hypertarget{#1}{\@subsubsection{#2}\label{subsec:#1}}
}

\notempty{\jku@lefthead}{
  \fancyhead[L]{\jku@lefthead}
}{}
\notempty{\jku@righthead}{
  \fancyhead[R]{\jku@righthead}
}{}
\notempty{\jku@leftfoot}{
  \fancyfoot[L]{JKU/\jku@leftfoot}
}{}
\notempty{\jku@rightfoot}{
  \fancyfoot[R]{\jku@rightfoot}
}{}
\AtBeginDocument{\pagestyle{fancy}}

\ifjku@tightlists
  \AtBeginEnvironment{itemize}{
    \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}
  }
\fi

\notblank{\jku@parspacing}{
  \IfFileExists{parskip.sty}{%
    \RequirePackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{\jku@parspacing plus 2pt minus 1pt}
  }
}{}

\notblank{\jku@linespacing}{
  \AtBeginDocument{\setstretch{\jku@linespacing}}
}{}

\endinput
  • 1
    I'm using \DeclareBoolOption{graphics} now, which has the same effect as your suggestion. – vbwx Jan 3 at 21:09
  • @vbwx: Yes, that's another possibility, of course! Didn't thought of that – user31729 Jan 3 at 21:11

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.