在加载包之前使用用户定义的变量

在加载包之前使用用户定义的变量

我正在尝试制作一个模板,在其中可以指定草稿版本,并且页脚中会出现一些文本,指出这是草稿,日期为今天。我还定义了一个函数,允许将文本包裹在 \isdraft{stuff} 中,并且只有在草稿中才会呈现“stuff”。我使用下面的代码让它工作。我的问题是,我试图允许一个特殊的草稿版本“POS”,它使用参数 showframe 加载几何包。这不起作用,我猜是因为首先评估了 draftver 的默认值,当 TeX 去更正它时,它已经加载了几何。

我的问题是:有没有办法在加载 \geometry 之前捕获 \draftver 中的变化,而无需将几何图形移出类文件?

第二个问题:我可能能够通过在主文档中的类调用上方定义 \draftver 来使其工作,但如果未定义 draftver,则类将中断。我如何确定它是否已定义,如果没有定义它,如何定义它?谢谢!

在我的主文件中,顶部有以下内容:\documentclass[12pt]{MyClass} \renewcommand{\draftver}{POS}

在类文件中,我有以下内容:

% ========================================== SET DRAFT =========================================== %
% This section defines the logic for making a draft. We do this in two parts:
% 1. Create a "variable" called draftver (as in draft version). If the user sets it to something,
  % then the footers will contain the text "DRAFT \draftver - Last edited \today". Note that it's
  % important to call this the same thing the user can \renewcommand in the preamble, otherwise any
  % change to it won't be picked up in this class file (to make heading appear, for example).
% 2. Create a "function" checker for whether it's a draft, and then put whatever they specify it it
  % is. This "anything" is the argument to the command.
% https://tex.stackexchange.com/questions/239305, https://stackoverflow.com/questions/2144176
  % http://ctan.math.utah.edu/ctan/tex-archive/macros/generic/xstring/xstring_doc_en.pdf

\newcommand\draftver{FINAL}  % Sets the "default" value to "FINAL"

% Print whatever they feed into argument 1 if it is a draft (meaning not omitted, FINAL, or REVIEW);
  % otherwise put nothing. The code below checks first whether the draft version is FINAL (if the
  % draft line is omitted, the line above sets the version to FINAL) or REVIEW. I tried to account 
  % for changes in capitalization, but \lowercase{\draftver} and \uppercase{\draftver} were not 
  % reliable, even when I did the same casing to the word to compare. I tried a few other things,
  % but all were too unreliable, so you're forced to just type FINAL or REVIEW in all caps. Sorry.
\newcommand{\ifdraft}[1]{
  \IfStrEq{\draftver}{FINAL} {%
    % If yes, we return nothing where this was called
  } {%
    % If not, check whether they specified a draft version or we are in REVIEW
    \IfStrEq{\draftver}{REVIEW} {%
      % If review, we return nothing where this was called
    } {%
      % If not FINAL or REVIEW versions, then we print whatever they specified
      #1
    }
  } 
}

% ========================================== DOC SETUP =========================================== %
% This section defines some basic things about the document, including margins, paper size, header
  % and footer, table of contents, etc.

% Allow user to show the boxes around margins and things to help in positioning images and tables and so forth. To do this, we check for the substring "show" in the draft name.
\IfStrEq{\draftver}{POS} {%
  % If yes, we set the geometry (margins) of the page such that it draws the boxes
  \geometry{letterpaper, left= 1.2in, right=1in, bottom=1in, showframe}
} {%
  % If not we simply set the margins
  \geometry{letterpaper, left= 1.2in, right=1in, bottom=1in}
} 

% ==------------------------------------- Header / Footer --------------------------------------== %
% Set page number to top right. Fancy is not used currently.
\pagestyle{fancy}
\fancyhf{}
\fancyfoot{}
\IfStrEq{\draftver}{FINAL}{}{%
  % If version is anything but FINAL (or omitted):
  \fancyfoot[c]{\textit{ DRAFT \draftver \hspace{0.4em}- last edited \today} }
}
\fancyhead{}
\renewcommand{\headrulewidth}{0pt}
\fancyhead[R]{\thepage}

% Set page number to top right. Plain is used by default.
\fancypagestyle{plain}{\fancyhf{}
\fancyfoot{}
\IfStrEq{\draftver}{FINAL}{}{%
  % If version is anything but FINAL (or omitted):
  \fancyfoot[c]{\textit{ DRAFT \draftver \hspace{0.4em}- last edited \today} }
}
\fancyhead{}
\renewcommand{\headrulewidth}{0pt}
\fancyhead[R]{\thepage}}

答案1

我猜想这些包是在主程序中读取 \draftver 之前加载的。最简单的解决方案是使用变量加载几何图形,然后在 draftver 下方添加 \newgeometry 开关,用户可以注释或取消注释以查看框。我最终使用了这个。

类文件:

\newcommand\leftMargin{1.2in}
\newcommand\rightMargin{1.0in}
\newcommand\bottomMargin{1.0in}
\geometry{letterpaper, 
  left= \leftMargin,
  right=\rightMargin,
  bottom=\bottomMargin
}

主文本:

\renewcommand{\draftver}{1.0}
% Uncomment this line to show boxes around margins for positioning
%\newgeometry{left=\leftMargin,right=\rightMargin,bottom=\bottomMargin,showframe}

这样,草稿做出的视觉更改就无关紧要了,而且我仍然有一个更改边距的位置。虽然这不能直接回答我的问题,但它完成了工作!

相关内容