4

I tried to create my custom class for lab reports, which needs to have a tabular on the titlepage which contains the dates on that I gave the report to my assistant for correction. In order to make things for the user as easy as possible I wanted to appear the dates and text automatically when they are defined by a command like \firsthandin{2017-01-06}. As I need to define \firsthandin, my thought was that it is useless to check whether this macro is defined, it is of course; but rather whether the argument is empty or differs from the default value. So I tried using \ifmtarg from ifmtarg.sty. Here is what I have so far:

protokoll.cls

\ProvidesClass{protokoll}[2017/01/05 v1.0]
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{scrartcl}}
\ProcessOptions
\LoadClass[a4paper,fontsize=11pt,oneside]{scrartcl}
%=====================================================
\RequirePackage[utf8]{inputenc}
\RequirePackage{ifmtarg}
%=====================================================
\newcommand{\@firsthandin}{}
\newcommand{\firsthandin}[1]{\renewcommand{\@firsthandin}{#1}}

\newcommand{\@secondhandin}{Unknown}
\newcommand{\secondhandin}[1]{\renewcommand{\@secondhandin}{#1}}

\renewcommand{\maketitle}{
\begin{titlepage}
\begin{table}
\centering
\begin{tabular}{ll}
\ifmtarg{\@firsthandin}{}{First date} & \ifmtarg{\@firsthandin{}{\@firsthandin} \\
Second date & \@secondhandin \\
\end{tabular}
\end{table}
\end{titlepage}
}

The second line is just to check whether there is another mistake regarding the variables. I also tried changing the first line of the tabular to \ifmtarg{\@firsthandin}{}{First date & \@firsthandin \\} but this produces the same errors.

mwe_protokoll.tex

\documentclass{protokoll}
\firsthandin{05.01.2017}
\begin{document}
\maketitle
\end{document}

I get errors about Misplaced \cr which is according to @DavidCarlisle (What does Misplaced \cr in latex error mean) used in the definition of \\, so I guess \ifmtarg messes up with the linebreaks of the tabular. Any ideas how to solve this? (It is not important whether an empty space or "really nothing" is given out)

  • 3
    The command is \@ifmtarg, but there is another issue with \noalign, I suppose – user31729 Jan 6 '17 at 14:05
  • 1
    why not use etoolbox its macro examination macros are much more power full. Plus it is meant to he used in user space (aka no @ in macro names) – daleif Jan 6 '17 at 14:08
  • @daleif: The \@ifmtarg is not in user space here, it's class code (but I agree with you about the possibly easier etoolbox approach) – user31729 Jan 6 '17 at 14:18
  • 1
    @ChristianHupfer: Yes the missing @ was actually the problem...quite easy and a quite stupid mistake from me. But thanks for your eagle eyes. I will accept your answer as you solved my problem but I think I will use the etoolbox method because it is even easier. – Enno Jan 6 '17 at 14:23
  • @ChristianHupfer you're right, but it seems I do not need the \edef part in the version I posted. – daleif Jan 6 '17 at 14:39
5

Conditionals in tabular cells are tricky. They should be expanded first using something like \edef\x{\if...}\x

I did not change the driver file protokoll.tex, so please load from the O.P.

\ProvidesClass{protokoll}[2017/01/05 v1.0]
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{scrartcl}}
\ProcessOptions
\LoadClass[a4paper,fontsize=11pt,oneside]{scrartcl}
%=====================================================
\RequirePackage[utf8]{inputenc}
\RequirePackage{ifmtarg}
%=====================================================
\newcommand{\@firsthandin}{}
\newcommand{\firsthandin}[1]{\renewcommand{\@firsthandin}{#1}}

\newcommand{\@secondhandin}{Unknown}
\newcommand{\secondhandin}[1]{\renewcommand{\@secondhandin}{#1}}

\renewcommand{\maketitle}{
\begin{titlepage}
\begin{table}
\centering
\begin{tabular}{ll}
  \edef\x{\@ifmtarg{\@firsthandin}{}{First Date}}\x & \edef\x{\@ifmtarg{\@firsthandin}{}{\@firsthandin}}\x \\
  Second date & \@secondhandin \\
\end{tabular}
\end{table}
\end{titlepage}
}

enter image description here

5

Here is an example using etoolbox, I've also renamed the holder macros so they can be used in user space for this example.

Note that etoolbox contains a \ifblank macro, but that cannot be used to test the emptiness of a given macro.

\documentclass[a4paper]{article}

\usepackage{etoolbox}
\newcommand{\firsthandinXX}{}
\newcommand{\firsthandin}[1]{\renewcommand{\firsthandinXX}{#1}}

\newcommand{\secondhandinXX}{Unknown}
\newcommand{\secondhandin}[1]{\renewcommand{\secondhandinXX}{#1}}

\renewcommand{\maketitle}{
\begin{titlepage}
\begin{table}
\centering
\begin{tabular}{ll}
\ifdefvoid{\firsthandinXX}{}{First date} & \ifdefvoid{\firsthandinXX}{}{\firsthandinXX} \\
Second date & \secondhandinXX \\
\end{tabular}
\end{table}
\end{titlepage}
}


\firsthandin{05.01.2017}

\begin{document}

\maketitle

\end{document}
2

You have a few errors in your code: check the differences.

protokoll.cls

\ProvidesClass{protokoll}[2017/01/05 v1.0]
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{scrartcl}}
\ProcessOptions
\LoadClass[a4paper,fontsize=11pt,oneside]{scrartcl}
%=====================================================
\RequirePackage[utf8]{inputenc}
\RequirePackage{ifmtarg}
%=====================================================
\newcommand*{\@firsthandin}{}
\newcommand{\firsthandin}[1]{\renewcommand{\@firsthandin}{#1}}

\newcommand{\@secondhandin}{Unknown}
\newcommand{\secondhandin}[1]{\renewcommand{\@secondhandin}{#1}}

\renewcommand{\maketitle}{%
\begin{titlepage}
  \centering
  \begin{tabular}{ll}
  \expandafter\@ifnotmtarg\expandafter{\@firsthandin}{First date & \@firsthandin \\}%
  Second date & \@secondhandin \\
  \end{tabular}
  \end{titlepage}%
}

No need for a table environment, first of all; then the correct command is \@ifmtarg, but \@ifnotmtarg is easier here; you were missing a brace, too.

You need to expand \@firsthandin in order to see whether it is empty or not.

mwe.tex

\documentclass[a4paper,fontsize=11pt,oneside]{protokoll}

\firsthandin{05.01.2017}

\begin{document}

\maketitle

\end{document}

Output

enter image description here

  • Oh you are right, \@ifnotmtarg is in fact easier. Thanks for your improvements. – Enno Jan 6 '17 at 15:21

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.