4

I want to create my own documentclass and use some variables for creating titlepages etc with it.

So what's the best way/package for doing this?

the class file:

\ProvidesClass{myOwnClass}[v0.1]
\newcommand{\myTitlepage}{
    \begin{titlepage}
        \begin{flushright}
        \textsf{
            \thisAutor\\
            E-Mail: \href{mailto:\thisEmail}{\thisEmail}\\[2ex]
            \ifdefined{Phone: \thisPhone}
            \today
            }
        \end{flushright}
    \end{titlepage}
}

the tex-File:

\documentclass{myOwnClass}

% BEGIN_FOLD
% Titel
\newcommand{\thisTitle}{Test}
\newcommand{\thisSubTitle}{a test for \LaTeX}

% Autor
\newcommand{\thisAutor}{Some Name}
\newcommand{\thisEmail}{somename@someprovider.com}
\newcommand{\thisPhone}{}
% END_FOLD

\begin{document}
\myTitlepage
\end{document}

You see, there is no phonenumber given. If it is so I don't want to print this row, but if there is a given phone number it want to print it. What's the best way to do this?

Thanks!

Phab
  • 161

1 Answers1

4

Making users having \newcommand{\thisAuthor}{Some Name} is not a good interface. A better one would be having, in the class file,

\newcommand{\Author}[1]{\renewcommand{\phab@Author}{#1}}
\let\phab@Author\phab@required
\newcommand{\Email}[1]{\renewcommand{\phab@Email}{#1}}
\let\phab@Email\@empty
\newcommand{\Phone}[1]{\renewcommand{\phab@Phone}{#1}}
\let\phab@Phone\@empty

so that you can have something like

\ifx\phab@Phone\@empty
  % do nothing
\else
  Phone: \phab@Phone\\[2ex]
\fi

in your definition of \myTitlepage. If you give a suitable definition of \phab@required you can issue an error if \Author is not in the document. In the document you'll type

\Author{Some Name}
\Email{somename@someprovider.com}

and if \Phone doesn't appear or there is \Phone{}, nothing will be printed.

Here's an example.

Class file myownclass.cls

\ProvidesClass{myownclass}[2014/05/06 v0.1]
\newcommand{\myTitlepage}{%
  \begin{titlepage}
  \raggedleft\sffamily
    {\Large\phab@Title\\}
    \ifx\phab@SubTitle\@empty
    \else
      \vspace{1ex}
      \phab@SubTitle\\
    \fi
    \vspace{4ex}
    \phab@Author\\
    \ifx\phab@Email\@empty
    \else
      E-Mail: \href{mailto:\phab@Email}{\phab@Email}\\[2ex]
    \fi
    \ifx\phab@Phone\@empty
    \else
      Phone: \phab@Phone\\[2ex]
    \fi
    \today
  \end{titlepage}
}

\newcommand{\Title}[1]{\renewcommand{\phab@Title}{#1}}
\newcommand{\SubTitle}[1]{\renewcommand{\phab@SubTitle}{#1}}
\newcommand{\Author}[1]{\renewcommand{\phab@Author}{#1}}
\newcommand{\Email}[1]{\renewcommand{\phab@Email}{#1}}
\newcommand{\Phone}[1]{\renewcommand{\phab@Phone}{#1}}

% Initializations
\newcommand\phab@required[1]{The field `#1' is required}

\newcommand\phab@Title{\phab@required{Title}}
\let\phab@SubTitle\@empty
\newcommand\phab@Author{\phab@required{Author}}
\let\phab@Email\@empty
\let\phab@Phone\@empty

\LoadClass{article}

\AtBeginDocument{\RequirePackage{hyperref}}

\endinput

Document phab.tex

\documentclass{myownclass}

\Title{Test}
\SubTitle{a test for \LaTeX}

% Author
\Author{Some Name}
\Email{somename@someprovider.com}
\Phone{}
% END_FOLD

\begin{document}
\myTitlepage
\end{document}

Output

enter image description here

egreg
  • 1,121,712
  • Thanks, your solution seems to be fine. But I always get the error "\phab@Author undefined. \Author{Some Name}". – Phab May 06 '14 at 15:12
  • @Phab Did you provide a definition for \phab@required? It should go before the definition of \Author. – egreg May 06 '14 at 15:14
  • And when I have \Phone{} (with no number), "Phone:" is still printed. – Phab May 06 '14 at 15:18
  • How do I have to do this definition in my text? – Phab May 06 '14 at 15:20
  • @Phab I added a complete example – egreg May 06 '14 at 15:54
  • Sorry, but even your complete example does not work for me. I copied the code and the first shot was as your output shows. But if I entered a phone number, no phone numer was printed, and if I canceled out the email, "E-Mail:" was still printed. – Phab May 07 '14 at 06:55
  • It certainly depends on the (missing) "@empty" – Phab May 07 '14 at 07:31
  • @Phab Yes, sorry, there was a missing \@empty. However, if I remove the \Email line, nothing is printed. – egreg May 07 '14 at 08:44
  • Ok if I comment this lines I want to be empty it works fine. But what if the user just doesn't fill out the line: "\Email{}". In this case I want nothing to be printed. With the actual code I'll get "E-Mail:" – Phab May 07 '14 at 12:35