클래스 내부에 설정된 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행에서 a를 반환하는 이유는 무엇입니까?

  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}

관련 정보