[Tex/LaTex] How to pass title (or other parameter) to class file

class-optionsdocument-classesdocumentclass-writing

I know LaTeX just to the point of making simple documents and modifying simple characteristics of a document's layout. Now I'm creating a class for a kind of document in order not to have to copy a huge preamble in every document, and I don't know to achieve what I want to do. First of all, the class inherits from the article class, uses otf fonts so I'm compiling with XeLaTeX, and uses the fancyhdr package.

I want to specify the fancyhdr headers inside the .cls file because they're all going to have the same style, so I would like to include in the .cls file the lines:

\lhead[<even output>]{<odd output>}
\chead[<even output>]{<odd output>}
\rhead[<even output>]{<odd output>}

What's inside, though, would have to be parameters that would be specified in the .tex file, like the document's title, or the author's name, or other stuff. How could I pass this kinds of arguments to the .cls file. If a general solution is given (in order to learn LaTeX besides being able to do what I want), I would very much appreciate it too.

Thank you very much.

EDIT:

As it can be seen in the article class file article.cls the above parameters can be called via \@author, \@title and so on. The below answer by Christian answers what I asked in the comments, and shows how to create different parameters in the class file that could be defined from the .tex file.

Best Answer

The class

\ProvidesClass{mysuperclass}
\LoadClass{article}

\RequirePackage{xkeyval}%

\def\docauthorname{A. U. Thor}%
\def\docname{\jobname.tex}%
\def\docdate{\today}%


\define@key{mysuperclass.cls}{docdate}[\today]{%
  \def\docdate{#1}%
}

\define@key{mysuperclass.cls}{author}[A. U. Thor]{%
  \def\docauthorname{#1}%
}
\define@key{mysuperclass.cls}{docname}[]{%
  \def\docname{#1}%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ExecuteOptionsX{author,docdate}
\ProcessOptionsX%

\RequirePackage{fancyhdr}

\AtBeginDocument{%
\lhead[\docauthorname]{\docauthorname}%
\chead[\docdate]{\docdate}%
\rhead[]{\docname}%
\pagestyle{fancy}%
}
\endinput

The class driver file:

\documentclass[docdate=1/1/2016,author=Me,docname=yelostfile]{mysuperclass}

\usepackage{blindtext}
\begin{document}
\blindtext[15]
\end{document}
Related Question