クラス内に設定された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}

それに関して、私は2つの質問があります。

  1. ドキュメントはコンパイルされるのに、Undefined control sequence. }14 行目で が返されるのはなぜですか?

  2. \hypersetupメインファイルではなく、クラス(変数を含む)に移動することは可能ですか?

PDFTeX と XeTeX で試しました。

答え1

コード内に未定義のコマンドが 2 つあります。

  1. \phone:\newcommand{\phone}[1]{\def\@phone{#1}}クラス ファイルに追加します。
  2. \cs{ }: 単に削除するかpdftitle、有効なコマンドになるように修正します (ポイント 1 を参照)。

次の MWE の場合 (パッケージはfilecontentsこの MWE で両方の tex コードを 1 つのコンパイル可能な 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ときに、すべての etc. コマンドが確実に定義されます。\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}

関連情報