类设计:导入main.tex中定义的变量来定制表单输出?

类设计:导入main.tex中定义的变量来定制表单输出?

我创建了一个执行标准操作的类:声明和处理选项、加载基类(文章)、加载其他包、定义变量、定义自定义花式页眉和页脚,以及使用AtBeginDocumentAtEndDocument挂钩设置样式和表单布局,使用上面定义的变量进行自定义。

我的问题是:我如何在覆盖 中使用的默认类变量main.tex之前重新定义文件中的变量?目前,在 main 中设置变量不会覆盖默认值。以下是我希望通过此类实现的功能(表单自定义):\begin{document}AtEndDocument

\documentclass[color, 10pt, letterpaper]{coverletter}

\fromname{Bobby Digital}
\fromtitle{Futurist}

\toname{Duncan Merryweather}
\totitle{Knower of Things}
\tostreet{221b Baker Street}

\begin{document}

    The text here \ldots

\end{document}

理论上,表单会更新字段并正确显示。这是当前cls文件:

%% Identification
%% The class identifies itself and the LaTeX version needed
\NeedsTeXFormat{LaTeX2e}

\ProvidesClass{coverletter}[2018/01/03 Cover Letter by Adam Erickson]

%% Preliminary definitions needed by options

% For defining official university colors
\RequirePackage[dvinames]{xcolor}

%% Declare options
\DeclareOption{draft}{\OptionNotUsed}
\DeclareOption{landscape}{\OptionNotUsed}
\DeclareOption{titlepage}{\OptionNotUsed}

\DeclareOption{color}{
  \definecolor{color1}{RGB}{152,30,50}
  \definecolor{gray}{RGB}{94,106,113} % {HTML}{5e6a71}
}

\DeclareOption{bw}{
  \definecolor{color1}{RGB}{0,0,0}
  \definecolor{gray}{RGB}{94,106,113} % {HTML}{5e6a71}
}

\DeclareOption*{
  \ClassWarning{coverletter}{Unknown option '\CurrentOption'}
}

%\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}

\ExecuteOptions{color}

\ProcessOptions{\relax}

%% Load parent class with options
\LoadClassWithOptions{article}

%% Load packages

% UTF-8 input encoding
\RequirePackage[utf8]{inputenc}

% Measurements from university guide
\RequirePackage[top=2in, left=1.5in, bottom=0.5in, right=0.625in]{geometry}
\RequirePackage{graphicx}
\RequirePackage[colorlinks=false, pdfborder={0 0 0},]{hyperref}
\RequirePackage[absolute]{textpos}
\RequirePackage{ifthen}
\RequirePackage{soul}

% For placement of the horizontal line
\RequirePackage{tikz}

% A nice serif font (palatino), but not the prescribed nonfree ITC stone
\RequirePackage[sc,osf]{mathpazo}

% Header and footer formatting
\RequirePackage{fancyhdr}

%% Command definitions
\renewcommand{\date}[1][\today]{#1}

% Sender information
\newcommand{\fromname}[1][Bill Smith]{#1}
\newcommand{\fromtitle}[1][Researcher]{#1}
\newcommand{\fromdegree}[1][Ph.D.]{#1}
\newcommand{\fromdept}[1][Department of Bills]{#1}
\newcommand{\fromuni}[1][Bob University]{#1}
\newcommand{\fromstreet}[1][100 Bill Ave.]{#1}
\newcommand{\fromcity}[1][Miami, FL, 33101]{#1}
\newcommand{\fromcountry}[1][USA]{#1}
\newcommand{\fromtel}[1][(800) 123-4567]{#1}
\newcommand{\fromfax}[1]{#1}
\newcommand{\fromemail}[1][\url{[email protected]}]{#1}
\newcommand{\fromweb}[1][\url{https://www.bob.edu/smithlab/}]{#1}

% Recipient information
\newcommand{\toname}[1][Recipient]{#1}
\newcommand{\totitle}[1][Title]{#1}
\newcommand{\todept}[1][Department of \ldots]{#1}
\newcommand{\touni}[1][University of \ldots]{#1}
\newcommand{\tostreet}[1][Street]{#1}
\newcommand{\tocity}[1][City, State/Province, ZIP]{#1}
\newcommand{\tocountry}[1][Country]{#1}

% Opening and closing phrases
\newcommand{\opening}[1][Dear]{#1 \toname,}
\newcommand{\closing}[1][Sincerely]{#1,}

% Linked files
\newcommand{\signaturefile}{} % your signature
\newcommand{\logofile}{logo.eps} % university logo
\newcommand{\enclosure}{} % any enclosures

%% Body of the class, most of the declarations appear here

% Horizontal line, font, and header/footer style
\usetikzlibrary{calc}
\linespread{1.05}
%\pagestyle{fancy}

% Remove paragraph indentation
\setlength{\parindent}{0pt}
\setlength{\parskip}{0.8\baselineskip}
\raggedright

% Set the URL style
\urlstyle{sf}

% Ensure consistency of the footer
\fancypagestyle{main}{%
    \fancyhf{} % clear the existing header and footer
    % define the header
    \fancyhead[L]{%
        \begin{textblock*}{2in}[0.3066,0.39](1.5in,1.33in)
            \includegraphics[width=2in]{\logofile}
        \end{textblock*}
        \begin{textblock*}{6.375in}(1.5in,1.4in) % 6.375 = 8.5-1.5-0.625
            \sffamily
            \hfill \color{gray} \fromdept
            \\ \hfill \fromname, \fromdegree
        \end{textblock*}
        \begin{tikzpicture}[remember picture,overlay]
            \draw[color=color1,line width=0.7pt] (current page.north west)+(1.5in,-1.33in) -- ($(-0.625in,-1.33in)+(current page.north east)$);
        \end{tikzpicture}
    }
    % define the footer
    \fancyfoot[L]{%
        {\footnotesize \color{gray} \sffamily
        \fromstreet, \fromcity \\
        [-0.1\baselineskip]
        \fromtel\ \textbullet\ Fax: \fromfax\ \textbullet\ \fromemail\ \textbullet\ \fromweb} \color{black}
    }
    % define the line thickness
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0pt}
}

\AtBeginDocument{% Text lines should be less than 6in long
    \pagestyle{main} % fancy page style
    \newgeometry{top=2in,left=1.5in,bottom=1.2in,right=1in}
    \date \\ \bigskip
    \toname\ifthenelse{\equal{\toname}{}}{}{\\}
    \todept \\
    \touni \\
    \tostreet \\
    \tocity \\
    \tocountry \\
    \bigskip
    \opening\par
}

\AtEndDocument{%
    \par\vspace{2ex}
    \closing\par
    \ifthenelse{\equal{\signaturefile}{}}{\bigskip\bigskip}{\includegraphics[width=1.2in]{\signaturefile}\\[-0.2\baselineskip]}
    \ifthenelse{\equal{\fromdegree}{Ph.D.}}{Dr.~\fromname}{\fromname} \\
    \fromtitle\ifthenelse{\equal{\fromtitle}{}}{}{\\}
    \enclosure
}

\endinput

再次,main.text文件如上。

答案1

在此处输入图片描述

我建议使用传统的\@author等方法\maketitle,即定义存储宏\fromname,设置\@fromname用于信头等的方法。

例子:

\def\@fromname{Bill Smith}% Default value. 

\newcommand{\fromname}[1]{%
   \gdef\@fromname{#1}%
}

类代码(部分内容已注释掉)

%% Identification
%% The class identifies itself and the LaTeX version needed
\NeedsTeXFormat{LaTeX2e}

\ProvidesClass{coverletter}[2018/01/03 Cover Letter by Adam Erickson]

%% Preliminary definitions needed by options

% For defining official university colors
\RequirePackage[dvinames]{xcolor}

%% Declare options
\DeclareOption{draft}{\OptionNotUsed}
\DeclareOption{landscape}{\OptionNotUsed}
\DeclareOption{titlepage}{\OptionNotUsed}

\DeclareOption{color}{
  \definecolor{color1}{RGB}{152,30,50}
  \definecolor{gray}{RGB}{94,106,113} % {HTML}{5e6a71}
}

\DeclareOption{bw}{
  \definecolor{color1}{RGB}{0,0,0}
  \definecolor{gray}{RGB}{94,106,113} % {HTML}{5e6a71}
}

\DeclareOption*{
  \ClassWarning{coverletter}{Unknown option '\CurrentOption'}
}

%\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}

\ExecuteOptions{color}

\ProcessOptions{\relax}

%% Load parent class with options
\LoadClassWithOptions{article}

%% Load packages

% UTF-8 input encoding
\RequirePackage[utf8]{inputenc}

% Measurements from university guide
\RequirePackage[top=2in, left=1.5in, bottom=0.5in, right=0.625in]{geometry}
\RequirePackage{graphicx}
\RequirePackage[absolute]{textpos}
\RequirePackage{ifthen}
\RequirePackage{soul}

\RequirePackage{etoolbox}
\RequirePackage[colorlinks=false, pdfborder={0 0 0},]{hyperref}

% For placement of the horizontal line
\RequirePackage{tikz}

% A nice serif font (palatino), but not the prescribed nonfree ITC stone
\RequirePackage[sc,osf]{mathpazo}

% Header and footer formatting
\RequirePackage{fancyhdr}

%% Command definitions
\renewcommand{\date}[1][\today]{#1}

% Sender information

\gdef\@fromname{Bill Smith}

\newcommand{\fromname}[1]{%%
  \gdef\@fromname{#1}%
}

\gdef\@toname{Recipient}

\newcommand{\toname}[1]{%
  \gdef\@toname{#1}%
}


% Opening and closing phrases
\newcommand{\opening}[1][Dear]{#1 \@toname,}
\newcommand{\closing}[1][Sincerely]{#1,}

% Linked files
\newcommand{\signaturefile}{} % your signature
\newcommand{\logofile}{ente} % university logo

%\newcommand{\logofile}{logo.eps} % university logo
\newcommand{\enclosure}{} % any enclosures

%% Body of the class, most of the declarations appear here

% Horizontal line, font, and header/footer style
\usetikzlibrary{calc}
\linespread{1.05}
%\pagestyle{fancy}

% Remove paragraph indentation
\setlength{\parindent}{0pt}
\setlength{\parskip}{0.8\baselineskip}
\raggedright

% Set the URL style
\urlstyle{sf}

% Ensure consistency of the footer
\fancypagestyle{main}{%
    \fancyhf{} % clear the existing header and footer
    % define the header
    \fancyhead[L]{%
        \begin{textblock*}{2in}[0.3066,0.39](1.5in,1.33in)
            \includegraphics[width=2in]{\logofile}
        \end{textblock*}
        \begin{textblock*}{6.375in}(1.5in,1.4in) % 6.375 = 8.5-1.5-0.625
            \sffamily
            \hfill \color{gray} %\fromdept
            \\ \hfill %\@fromname, %\fromdegree
        \end{textblock*}
        \begin{tikzpicture}[remember picture,overlay]
            \draw[color=color1,line width=0.7pt] (current page.north west)+(1.5in,-1.33in) -- ($(-0.625in,-1.33in)+(current page.north east)$);
        \end{tikzpicture}
    }
    % define the footer
%    \fancyfoot[L]{%
%      {\footnotesize \color{gray} \sffamily
%          % \fromstreet, \fromcity 
%        \\[-0.1\baselineskip]
%        \fromtel\ \textbullet\ Fax: \fromfax\ \textbullet\ \fromemail\ \textbullet\ \fromweb} \color{black}
%    }
    % define the line thickness
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0pt}
}

\AtBeginDocument{% Text lines should be less than 6in long
    \pagestyle{main} % fancy page style
    \newgeometry{top=2in,left=1.5in,bottom=1.2in,right=1in}
    \date \\ \bigskip
    %\toname\ifthenelse{\equal{\toname}{}}{}{\\}
    %\todept \\
    %\touni \\
    %\tostreet \\
    %\tocity \\
    %\tocountry \\
    \bigskip
    \opening\par
}

\AtEndDocument{%
    \par\vspace{2ex}
    \closing\par
    \ifthenelse{\equal{\signaturefile}{}}{\bigskip\bigskip}{\includegraphics[width=1.2in]{\signaturefile}\\[-0.2\baselineskip]}
%    \ifthenelse{\equal{\fromdegree}{Ph.D.}}{Dr.~\fromname}{\fromname} \\
%    \fromtitle\ifthenelse{\equal{\fromtitle}{}}{}{\\}
    \enclosure
}

\endinput

主要驱动因素:

\documentclass[color, 10pt, letterpaper]{coverletter}

\fromname{Bobby Digital}
\toname{Duncan Merryweather}
%\fromtitle{Futurist}

%\totitle{Knower of Things}
%\tostreet{221b Baker Street}

\begin{document}

    The text here \ldots

\end{document}

**更新l3keys**界面

    \documentclass[color, 10pt, letterpaper]{coverletter}


    \SetLetterData{fromname={Bobby Digital},
      fromtitle={Futurist},
      toname={Duncan Merryweather},
      totitle={Knower of Things},
      tostreet={221b Baker Street}
    }

    \begin{document}

        The text here \ldots

    \end{document}

新班级代码:

%% Identification
%% The class identifies itself and the LaTeX version needed
\NeedsTeXFormat{LaTeX2e}

\ProvidesClass{coverletter}[2018/01/03 Cover Letter by Adam Erickson]

%% Preliminary definitions needed by options

% For defining official university colors
\RequirePackage[dvinames]{xcolor}

%% Declare options
\DeclareOption{draft}{\OptionNotUsed}
\DeclareOption{landscape}{\OptionNotUsed}
\DeclareOption{titlepage}{\OptionNotUsed}

\DeclareOption{color}{
  \definecolor{color1}{RGB}{152,30,50}
  \definecolor{gray}{RGB}{94,106,113} % {HTML}{5e6a71}
}

\DeclareOption{bw}{
  \definecolor{color1}{RGB}{0,0,0}
  \definecolor{gray}{RGB}{94,106,113} % {HTML}{5e6a71}
}

\DeclareOption*{
  \ClassWarning{coverletter}{Unknown option '\CurrentOption'}
}

%\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}

\ExecuteOptions{color}

\ProcessOptions{\relax}

%% Load parent class with options
\LoadClassWithOptions{article}

%% Load packages

% UTF-8 input encoding
\RequirePackage[utf8]{inputenc}

% Measurements from university guide
\RequirePackage[top=2in, left=1.5in, bottom=0.5in, right=0.625in]{geometry}
\RequirePackage{graphicx}
\RequirePackage[absolute]{textpos}
\RequirePackage{ifthen}
\RequirePackage{soul}

\RequirePackage{etoolbox}
\RequirePackage[colorlinks=false, pdfborder={0 0 0},]{hyperref}

% For placement of the horizontal line
\RequirePackage{tikz}

% A nice serif font (palatino), but not the prescribed nonfree ITC stone
\RequirePackage[sc,osf]{mathpazo}

% Header and footer formatting
\RequirePackage{fancyhdr}

%% Command definitions
\renewcommand{\date}[1][\today]{#1}

\usepackage{xparse} 


\ExplSyntaxOn 

\prop_new:N \l_coverletter_data_prop

\keys_define:nn {coverletter} {%
  fromname .code:n={ \prop_put:Nnn \l_coverletter_data_prop {fromname} {#1} },
  fromtitle .code:n={ \prop_put:Nnn \l_coverletter_data_prop {fromtitle} {#1} },
  fromdegree .code:n={ \prop_put:Nnn \l_coverletter_data_prop {fromdegree} {#1} },
  fromdept .code:n={ \prop_put:Nnn \l_coverletter_data_prop {fromdept} {#1} },
  fromuni .code:n={ \prop_put:Nnn \l_coverletter_data_prop {fromuni} {#1} },
  fromcity .code:n={ \prop_put:Nnn \l_coverletter_data_prop {fromcity} {#1} },
  fromstreet .code:n={ \prop_put:Nnn \l_coverletter_data_prop {fromstreet} {#1} },
  fromtel .code:n={ \prop_put:Nnn \l_coverletter_data_prop {fromtel} {#1} },
  fromcountry .code:n={ \prop_put:Nnn \l_coverletter_data_prop {fromcountry} {#1} },
  fromfax .code:n={ \prop_put:Nnn \l_coverletter_data_prop {fromfax} {#1} },
  fromemail .code:n={ \prop_put:Nnn \l_coverletter_data_prop {fromemail} {#1} },
  fromweb .code:n={ \prop_put:Nnn \l_coverletter_data_prop {fromweb} {#1} },
  %%% More to follow here
  % Now the to - stuff
  toname .code:n={ \prop_put:Nnn \l_coverletter_data_prop {toname} {#1} },
  totitle .code:n={ \prop_put:Nnn \l_coverletter_data_prop {totitle} {#1} },
  todept .code:n={ \prop_put:Nnn \l_coverletter_data_prop {todept} {#1} },
  touni .code:n={ \prop_put:Nnn \l_coverletter_data_prop {touni} {#1} },
  tocity .code:n={ \prop_put:Nnn \l_coverletter_data_prop {tocity} {#1} },
  tostreet .code:n={ \prop_put:Nnn \l_coverletter_data_prop {toreet} {#1} },
  tocountry .code:n={ \prop_put:Nnn \l_coverletter_data_prop {tocountry} {#1} },
}


\NewDocumentCommand{\SetLetterData}{+m}{%
  \keys_set:nn {coverletter} {#1}%
}

\cs_new:Npn \GetLetterData#1{%
  \prop_item:Nn \l_coverletter_data_prop {#1}
}
\ExplSyntaxOff

% Set some default data
\SetLetterData{fromname=Bill Smith, 
  fromtitle={Researcher},
  fromdegree={Ph.D}, 
  fromdept={Department of Bills},
  fromuni={Bob University},
  fromcity={Miami, FL, 33101},
  fromstreet={100 Bill Ave.},
  fromcountry={USA},
  fromtel={(800) 123-4567},
  fromfax={},
  fromemail={\url{[email protected]}},
  fromweb={\url{https://www.bob.edu/smithlab/}},
  toname={Recipient},
  totitle={Title},
  todept={Department of Idiots},
  touni={University of Fools},
  tostreet={3 Stooges Avenue},
  tocity={City, State/Province, ZIP},
  tocountry={Country}
}



% Opening and closing phrases
\newcommand{\opening}[1][Dear]{#1 \GetLetterData{toname},}
\newcommand{\closing}[1][Sincerely]{#1,}

% Linked files
\newcommand{\signaturefile}{} % your signature
\newcommand{\logofile}{ente} % university logo

%\newcommand{\logofile}{logo.eps} % university logo
\newcommand{\enclosure}{} % any enclosures

%% Body of the class, most of the declarations appear here

% Horizontal line, font, and header/footer style
\usetikzlibrary{calc}
\linespread{1.05}
%\pagestyle{fancy}

% Remove paragraph indentation
\setlength{\parindent}{0pt}
\setlength{\parskip}{0.8\baselineskip}
\raggedright

% Set the URL style
\urlstyle{sf}

% Ensure consistency of the footer
\fancypagestyle{main}{%
    \fancyhf{} % clear the existing header and footer
    % define the header
    \fancyhead[L]{%
        \begin{textblock*}{2in}[0.3066,0.39](1.5in,1.33in)
            \includegraphics[width=2in]{\logofile}
        \end{textblock*}
        \begin{textblock*}{6.375in}(1.5in,1.4in) % 6.375 = 8.5-1.5-0.625
            \sffamily
            \hfill \color{gray} \GetLetterData{fromdept}
            \\  \hfill \GetLetterData{fromname}, \GetLetterData{fromdegree}
        \end{textblock*}
        \begin{tikzpicture}[remember picture,overlay]
            \draw[color=color1,line width=0.7pt] (current page.north west)+(1.5in,-1.33in) -- ($(-0.625in,-1.33in)+(current page.north east)$);
        \end{tikzpicture}
    }
    % define the footer
    \fancyfoot[L]{%
      {\footnotesize \color{gray} \sffamily
           \GetLetterData{fromstreet}, \GetLetterData{fromcity} \\[-0.1\baselineskip]
           \GetLetterData{fromtel}\ \textbullet\ Fax: \GetLetterData{fromfax}\ \textbullet\ \GetLetterData{fromemail}\ \textbullet\ \GetLetterData{fromweb}} \color{black}
    }
    % define the line thickness
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0pt}
}

\AtBeginDocument{% Text lines should be less than 6in long
    \pagestyle{main} % fancy page style
    \newgeometry{top=2in,left=1.5in,bottom=1.2in,right=1in}
    \date 

    \bigskip
    \GetLetterData{toname}\ifthenelse{\equal{\GetLetterData{toname}}{}}{}{\\}

    \GetLetterData{todept} 

    \GetLetterData{touni} 

    \GetLetterData{tostreet} 

    \GetLetterData{tocity} 

    \GetLetterData{tocountry} 

    \bigskip
    \opening\par
}

\AtEndDocument{%
    \par\vspace{2ex}
    \closing\par
    \ifthenelse{\equal{\signaturefile}{}}{\bigskip\bigskip}{\includegraphics[width=1.2in]{\signaturefile}\\[-0.2\baselineskip]}
    \ifthenelse{\equal{\GetLetterData{fromdegree}}{Ph.D.}}{Dr.~\GetLetterData{fromname}}{\GetLetterData{fromname}} \\
    \GetLetterData{fromtitle}\ifthenelse{\equal{\GetLetterData{fromtitle}}{}}{}{\\}
    \enclosure
}

\endinput

解释:

新的 LaTeX3 具有良好的键值接口\keys_define:nn\keys_set:nn

对于每个相关的字母数据字段,我都定义了一个同名的键,即fromname等。

这些键存储在属性列表名称中\l_coverletter_data_prop,它有点像哈希表。

\SetLetterData{....}设置键并\GetLetterData{}提取键值。如果键值为空,则不返回任何内容,但如果\SetLetterData{}在类代码中使用一些默认值,则不会发生这种情况(除非忘记了某些键名)

相关内容