Переменные информации PDF, заданные внутри класса

Переменные информации PDF, заданные внутри класса

У меня есть простой пользовательский класс, 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}

В связи с этим у меня два вопроса:

  1. Почему документ компилируется, но возвращает Undefined control sequence. }в строке 14?

  2. Можно ли переместить \hypersetupв класс (с переменными), а не в основной файл?

Пробовал с PDFTeX и XeTeX.

решение1

В вашем коде есть две неопределенные команды:

  1. \phone: добавьте \newcommand{\phone}[1]{\def\@phone{#1}}в свой файл класса.
  2. \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}

вы не получаете никаких ошибок и результат:

полученный pdf

Чтобы переместить полную команду \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}

Связанный контент