我有一个简单的自定义类,mycustom.cls
定义为:
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{mycustom}[2019/03/06 My custom class]
\LoadClass{article}
\RequirePackage{hyperxmp}
\RequirePackage[unicode]{hyperref}
\newcommand{\name}[2]{
\def\@name{#1}
\def\@surname{#2}
\def\@fullname{#1 #2}
}
\newcommand{\address}[4]{
\def\@neighborhood{#1}
\def\@city{#2}
\def\@state{#3}
\def\@country{#4}
}
\endinput
还有一个最小的 TeX 文件mwe.tex
:
\documentclass{mycustom}
\name{John}{Smith}
\address{Spring Falls}{Helena-West Helena}{AR}{United States of America}
\makeatletter
\hypersetup{%
pdftitle={\@fullname \cs{ } title},
pdfsubject={\@neighborhood},
pdfauthor={\@fullname},
pdfcontactcity={\@city},
pdfcontactcountry={\@country},
pdfmetalang={en}
}
\makeatother
\begin{document}
foo bar barz
\end{document}
对此我有两个问题:
为什么文档可以编译但
Undefined control sequence. }
在第 14 行返回 a?是否可以将其移动
\hypersetup
到类(带有变量)而不是主文件?
尝试使用 PDFTeX 和 XeTeX。
答案1
您的代码中有两个未定义的命令:
\phone
:添加\newcommand{\phone}[1]{\def\@phone{#1}}
到你的课程文件中。\cs{ }
:只需将其删除pdftitle
或更正即可成为有效命令(参见第 1 点)。
使用以下 MWE(包filecontents
仅在该 MWE 中使用,以将两个 tex 代码连接到一个可编译的 MWE,您只需更改您的类文件即可):
\begin{filecontents*}{mycustom.cls}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{mycustom}[2019/03/06 My custom class]
\LoadClass{article}
\RequirePackage{hyperxmp}
\RequirePackage[unicode]{hyperref}
\newcommand{\name}[2]{
\def\@name{#1}
\def\@surname{#2}
\def\@fullname{#1 #2}
}
\newcommand{\address}[4]{
\def\@neighborhood{#1}
\def\@city{#2}
\def\@state{#3}
\def\@country{#4}
}
\newcommand{\phone}[1]{\def\@phone{#1}} % <=============================
\endinput
\end{filecontents*}
\documentclass{mycustom}
\name{John}{Smith}
\phone{+1 123 555-1234}
\address{Spring Falls}{Helena-West Helena}{AR}{United States of America}
\makeatletter
\hypersetup{%
% pdftitle={\@fullname \cs{ } title}, % <=============================
pdftitle={\@fullname title}, % <===================================
pdfsubject={\@neighborhood},
pdfauthor={\@fullname},
pdfcontactcity={\@city},
pdfcontactcountry={\@country},
pdfmetalang={en}
}
\makeatother
\begin{document}
foo bar barz
\end{document}
您不会收到任何错误,结果如下:
要将完整的命令移动\hypersetup
到类文件中,可以使用 command \AtBeginDocument{...}
。这确保在调用\@name
时定义了所有命令:\hypersetup
\begin{filecontents*}{mycustom.cls}
% https://tex.stackexchange.com/questions/478520/pdf-info-variables-set-inside-the-class
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{mycustom}[2019/03/06 My custom class]
\LoadClass{article}
\RequirePackage{hyperxmp}
\RequirePackage[unicode]{hyperref}
\newcommand{\name}[2]{%
\def\@name{#1}%
\def\@surname{#2}%
}
\def\@fullname{\@name\ \@surname}
\newcommand{\address}[4]{%
\def\@neighborhood{#1}%
\def\@city{#2}%
\def\@state{#3}%
\def\@country{#4}%
}
\newcommand{\phone}[1]{\def\@phone{#1}}
\AtBeginDocument{% <====================================================
\hypersetup{%
pdftitle={\@fullname\ title},
pdfsubject={\@neighborhood},
pdfauthor={\@fullname},
pdfcontactcity={\@city},
pdfcontactcountry={\@country},
pdfmetalang={en}
}
} % <===================================================================
\endinput
\end{filecontents*}
\documentclass{mycustom}
\usepackage[T1]{fontenc}
\name{John}{Smith}
\phone{+1 123 555-1234}
\address{Spring Falls}{Helena-West Helena}{AR}{United States of America}
\begin{document}
foo bar barz \makeatletter\@fullname\makeatother
\end{document}