3

Based on the answer I received to Print a short title in the header, I've created the macro \handoutheader in a .sty file I use as the template for my conference presentations.

\newcommand\@confname{} % create macro for name of conference
\newcommand\confname[1]{\renewcommand\@confname{#1}}
\newcommand\@confplace{} % create macro for place of conference
\newcommand\confplace[1]{\renewcommand\@confplace{#1}}
\newcommand\@confdate{} % create macro for date of conference
\newcommand\confdate[1]{\renewcommand\@confdate{#1}}
\newcommand{\handoutheader}{%
    My name \hfill \@confname\\
    My affiliation \hfill \@confplace\\
    My e-mail address \hfill \@confdate\\} % make handout header
\AtBeginDocument{\setlength{\parindent}{0pt}\handoutheader}

The logic is that I specify in the preamble of the .tex file for the handout what the name, place, and date of the conference is, and then the template creates a header on the handout with that information.

Now, I'd like to print the date of my presentation in the handout text. How do I do that? I've tried \confdate and \@confdate, but neither works.

\documentclass{article}
\usepackage{filecontents}

\begin{filecontents}{mystyle.sty}
%% My package starts here
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mystyle}
\newcommand\@confname{} % create macro for name of conference
\newcommand\confname[1]{\renewcommand\@confname{#1}}
\newcommand\@confplace{} % create macro for place of conference
\newcommand\confplace[1]{\renewcommand\@confplace{#1}}
\newcommand\@confdate{} % create macro for date of conference
\newcommand\confdate[1]{\renewcommand\@confdate{#1}}
\newcommand{\handoutheader}{%
    My name \hfill \@confname\\
    My affiliation \hfill \@confplace\\
    My e-mail address \hfill \@confdate\\} % make handout header
\AtBeginDocument{\setlength{\parindent}{0pt}\handoutheader}
\endinput
%% And ends here
\end{filecontents}

\usepackage{mystyle}
\confname{Name of conference}
\confplace{Place of conference}
\confdate{Date of conference}

\begin{document}
\vspace{2ex}\\
After hearing my talk, you will remember the date \confdate\ for the rest of your life.\\
After hearing my talk, you will remember the date \@confdate\ for the rest of your life.\\
\end{document} 

enter image description here

2

In your example, \confdate takes an argument. Therefore, when you use

...remember the date \confdate\ for the rest of your life.

the \ (control space) is grabbed as the argument and placed inside \@confdate. That's why your output resembles

...remember the date for the rest of your life.

On the other hand, when you use

...remember the date \@confdate\ for the rest of your life.

the interpretation is to call \@, which inserts a very large \spacefactor - and what remains is confdate . As such, the output is

...remember the date confdate for the rest of your life.

Ideally you should create yourself a macro that shows the "privilege content". In the case below, I've created \printdate which sets \@confdate - the macro the holds the date.

enter image description here

\documentclass{article}
\usepackage{filecontents}

\begin{filecontents}{mystyle.sty}
%% My package starts here
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mystyle}
\newcommand\@confname{} % create macro for name of conference
\newcommand\confname[1]{\renewcommand\@confname{#1}}
\newcommand\@confplace{} % create macro for place of conference
\newcommand\confplace[1]{\renewcommand\@confplace{#1}}
\newcommand\@confdate{} % create macro for date of conference
\newcommand\confdate[1]{\renewcommand\@confdate{#1}}
\newcommand{\printdate}{\@confdate}
\newcommand{\handoutheader}{%
    My name \hfill \@confname\\
    My affiliation \hfill \@confplace\\
    My date \hfill \@confdate\\} % make handout header
\AtBeginDocument{\setlength{\parindent}{0pt}\handoutheader}
\endinput
%% And ends here
\end{filecontents}

\usepackage{mystyle}
\confname{Name of conference}
\confplace{Place of conference}
\confdate{Date of conference}

\begin{document}

\bigskip

After hearing my talk, you will remember the date \printdate{} for the rest of your life.
\end{document}
3

The two main problems were:

  1. To use the @ in macro mames you need to use \makeatletter and \makeatother. See the references below.

  2. The definition

    \newcommand\confdate[1]{\renewcommand\@confdate{#1}}
    

    means that calling \confdate sets the value of \@confdate to the first parmamater. In your code the first line (which is commented out in the MWE below) was resetting the value of \@confdate to be blank, which was then printed out in the subsequent call to \@confdate. Comenting out that call fixes the problem.

enter image description here

Notes:

  • I'd recommend you not use \\ at the end of a new line. Just leave a blank line to indicate a new paragraph break.
  • If you don't want to use \makeatletter just don't use a macro name that has an @ in it, or define a macro such as \ShowConfDate shown in the MWE.

References

Code:

\documentclass{article}
\usepackage{filecontents}

\begin{filecontents}{mystyle.sty}
%% My package starts here
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mystyle}
\newcommand\@confname{} % create macro for name of conference
\newcommand\confname[1]{\renewcommand\@confname{#1}}

\newcommand\@confplace{} % create macro for place of conference
\newcommand\confplace[1]{\renewcommand\@confplace{#1}}
\newcommand\@confdate{} % create macro for date of conference
\newcommand\confdate[1]{\renewcommand\@confdate{#1}}
\newcommand\ShowConfDate{\@confdate}%

\newcommand{\handoutheader}{%
    My name \hfill \@confname\\
    My affiliation \hfill \@confplace\\
    My e-mail address \hfill \@confdate\\} % make handout header
\AtBeginDocument{\setlength{\parindent}{0pt}\handoutheader}
\endinput
%% And ends here
\end{filecontents}

\usepackage{mystyle}
\confname{Name of conference}
\confplace{Place of conference}
\confdate{Date of conference}

\begin{document}

\makeatletter
%After hearing my talk, you will remember the date \confdate\ for the rest of your life.
After hearing my talk, you will remember the date \@confdate\ for the rest of your life.
\makeatother

\medskip
If you don't want to be using \verb|\makeatletter| you can use \verb|\ShowConfDate| as in:
``The date is \ShowConfDate."
\end{document} 
  • That clearly works, but is there another way that doesn't require me to include macros like \makeatletter in the document text? I generally prefer to keep those in preambles and package files. EDIT: Never mind, I'm happy with \makeatletter\@confdate\makeatletter. – Sverre Oct 7 '14 at 17:15
  • @Sverre: Have updated solution to show that. – Peter Grill Oct 7 '14 at 17:27
2

If you don't need to allocate new name for printing version of the macro \confname, you can define more intelligent version of such macro: if the equal sign immediately follows then the value is stored else the value is used. Example:

The test: \confname\         % value is used, empty value is printed as default.
\confname={Name}             % the value "Name" is stored.
The second test: \confname\  % the value "Name" is printed.

The implementation looks like:

\def\confname{\futurelet\next\confnameA}
\def\confnameA{\ifx\next=\expandafter\confnameB \else \confnameC\fi}
\def\confnameB=#1{\def\confnameC{#1}}
\def\confnameC{}

But we are lazy to write this code three times (for \confname, \confplace, \confdate). So, we can create a more intelligent macro:

\def\sdef#1{\expandafter\def\csname#1\endcsname}
\def\setmacro#1{%
   \sdef{#1}{\expandafter\futurelet\expandafter\next\csname#1.A\endcsname}%
   \sdef{#1.A}{\ifx\next=\csname#1.B\expandafter\endcsname \else \csname#1.C\endcsname\fi}%
   \sdef{#1.B}=##1{\sdef{#1.C}{##1}}%
}
\setmacro{confname} \setmacro{confplace} \setmacro{confdate}

You can insert this code to your .sty file instead \newcommand\@confname...\newcommand\confdate and you can replace the \@confname etc. by \confname etc. in the body of your macro \handoutheader. And, of course, the usage is slightly different:

\confname={Name of conference}
\confplace={Place of conference}
\confdate={Date of conference}
  • Laziness is a virtue :-) – morbusg Oct 9 '14 at 9:50
  • I'm sure this is indeed more intelligent, which is probably why I don't understand much of the code ... :P – Sverre Oct 9 '14 at 12:16

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.