我有一個簡單的自訂類別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}