Setting up author or address string variables in LaTeX
Asked Answered
H

2

8

LaTeX is a wonderful language for writing documents. With the hyperref package and pdflatex, you easily generate documents with metadata, a nice feature to get your documents referenced right on the web.

I often use templates like:

\documentclass[11pt]{article}
\usepackage[pdftex, pdfusetitle,colorlinks=false,pdfborder={0 0 0}]{hyperref}%
\hypersetup{%
pdftitle={My title},%
pdfauthor={My name},%
pdfkeywords={my first keyword, my second keyword, more keywords.},%
}%
\begin{document}

\title{My title}
\author{My name}
\date{}
\maketitle

{\bf Keywords:} my first keyword, my second keyword, more keywords.%

My text is here...

\end{document}

So far, it's well. My question pops out from the example: is there a way to define string variables in the header so that they can be passed as arguments to hyperref and then to the frontmatter or to the text. Something like:

\documentclass[11pt]{article}
%-------definitions-----
\def\Author{My name}
\def\Title{My title}
\def\Keywords{my first keyword, my second keyword, more keywords.}
%--------------------------
\usepackage[pdftex, pdfusetitle,colorlinks=false,pdfborder={0 0 0}]{hyperref}%
\hypersetup{%
pdftitle={\Title},%
pdfauthor={\Author},%
pdfkeywords={\Keywords},%
}%
\begin{document}
\title{\Title}
\author{\Author}
\date{}
\maketitle

{\bf Keywords:} \Keywords %

My text is here...

\end{document}

This fails for the \maketitle part and for the hyperref metadata with ! Use of \Title doesn't match ! Argument of \let has an extra }.but also for including the keywords.

Hutt answered 25/2, 2010 at 17:14 Comment(3)
I believe in this case, the = after \def\Title acts as a parameter delimiter (though i'm not sure if it's allowed for no-parameter macros). That means you have to call \Title= instead of plain \Title for example. \Title = would work too (the delimiter is not part of the name). But it won't have the (maybe expected) meaning of an assignment.Downturn
Thanks, I corrected the typo! The question was open a total of 15 minutes!Hutt
I'm voting to close this question as off-topic because the problem cannot be reproduced under modern distributions.Creamer
H
13

The correct template should look like:

\documentclass[11pt]{article}
%-------definitions-----
\newcommand{\Author}{My name} 
\newcommand{\Title}{My title}
\newcommand{\Keywords}{my first keyword, my first keyword, more keywords.}
%--------------------------
\usepackage[pdftex, pdfusetitle,colorlinks=false,pdfborder={0 0 0}]{hyperref}%
\hypersetup{%
pdftitle={\Title},%
pdfauthor={\Author},%
pdfkeywords={\Keywords},%
}%
\begin{document}
\title{\Title}
\author{\Author}
\date{}
\maketitle
{\bf Keywords:} \Keywords %

My text is here...

\end{document}

Compiles fine and the metadata shows fine in the pdf reader.

Hutt answered 25/2, 2010 at 18:23 Comment(0)
I
3

Try using \newcommand{\Author}{My name} instead of \def.

Imco answered 25/2, 2010 at 17:21 Comment(1)
This should not make any difference.Creamer

© 2022 - 2024 — McMap. All rights reserved.