
クラスを使用し、いくつかのパッケージでタイプセッティングをカスタマイズするテンプレートを作成しています。次の直後に読み込まれるreport
別のファイルでいくつかのデータを定義しています:user-data.tex
documentclass
\def\thesis{Master} %<PhD> or <Master>
\def\thesistitle{The title of the thesis}
\def\author{AName AFirst ASecond}
\def\authormail{[email protected]}
\def\school{Master and Doctoral School}
\def\date{City, month year}
\def\logo-university{univ.pdf}
これらの変数/定数は、 とともにフッターに含められるかtitlesec
、ユーザーがいつでも使用できる 2 つの異なるタイトル ページを提供するために使用されます。
そのように( だけを使って)しても大丈夫でしょうか\def
? それとも\global
、\newcommand
... や他の何かを使うべきでしょうか? プレーンな 以外の形式で配置しますかtex
?
コメントの後、\newcommand*
いくつかのマクロを変更し、名前を変更しました。
\newcommand*\thesis{Master} %<PhD> or <Master>
\newcommand*\thesistitle{The title of the thesis}
\newcommand*\authors{AName AFirst ASecond}
\newcommand*\authorsmail{[email protected]}
\newcommand*\school{Master and Doctoral School}
\newcommand*\titledate{City, month year}
\newcommand*\university{univ.pdf}
@Andrewの回答を受けて、私は
\providecommand*\@school{No SCHOOL defined}
\newcommand*\School[1]{\renewcommand*\@school{#1}}
パッケージの冒頭で、ユーザーが\newcommand*\@school{Something}
ロードする前、または\School{Something}
ロードした後に定義できるようにします。
@barbara が推測したとおり、私はテンプレートを*.sty
パッケージとしてロードしている に移動しました:
マイパッケージ
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mypkg}
%If not previously defined, default value applied
\providecommand*{\@mythesis}{No THESIS TYPE defined}
\providecommand*{\@myauthor}{No AUTHOR defined}
%Command to modify the field at any point after this
\newcommand*\MyThesis[1]{\renewcommand*\@mythesis{#1}}
\newcommand*\MyAuthor[1]{\renewcommand*\@myauthor{#1}}
%Command to access the field
\newcommand*\showDF[1]{\csname @#1\endcsname}
%The content may be supplied in a separate file with \Thesis
\@input{data.dat}
\endinput
メイン.tex
\documentclass[a4paper,titlepage,11pt,twoside,openright]{report}
% A field may defined before loading the package
\makeatletter
\newcommand\@mythesis{My title before loading}
\makeatother
%If it not defined, the package does it
\usepackage{mypkg}
\begin{document}
% The variable may be directly accessed
\makeatletter\@mythesis\makeatother
% The content may be rewritten
\MyThesis{PhD}
% Reading with the provided command
\showDF{mythesis}
\showDF{myauthor}
\end{document}
動作します。ユーザーは、パッケージをロードする前、別のファイル、または「ヘルパー コマンド」を使用してロードした後の任意の時点で、コンテンツを定義できます。ただし、次の操作を実行したいです。
- コマンドを使用して、フィールドに関連付けられたコマンドを自動的に定義します。新しい質問ここ。
- パッケージをロードするときに、ユーザーがオプションとしてフィールドを指定できるようにします。私は次のように試しましたxkeyval
:
\define@key{fam}{thesis}[Master]{\Thesis{#1}}
\ProcessOptionsX<fam>
メインファイル内の読み込みを次のように変更します。
\usepackage[thesis=PhD]{mypkg}
上記のコードは、複数の単語を含むフィールドを除いて機能します。これは、フィールド間のスペースが削除されるためです。つまり、 で機能させるにはどうすればよいですか。と\usepackage[title=A title with several words]{mypkg}
で試しましたが、機能しません。{A title with several words}
"A title with several words"
このアプローチを使用するとこれ答えは、すべてのコマンドを一度に作成するための「ファクトリー」関数ができました。
\usepackage{etoolbox}
\newcommand\forcsvlistargs[2]{ \expandafter\providecommand\csname \detokenize{#1}aux\endcsname[1]{
\csname \detokenize{#1}\endcsname##1\relax}
\forcsvlist{\csname \detokenize{#1}aux\endcsname}{#2}
}
\newcommand\newDF[2]{ \expandafter\providecommand\csname th#1\endcsname{No \MakeUppercase{#2} defined}
\expandafter\newcommand\csname TH#2\endcsname[1]{\expandafter\renewcommand\csname th#1\endcsname{##1}} }
\forcsvlistargs{newDF}{{{typ}{type}}, {{date}{date}}, {{tit}{title}}, {{sch}{school}},
{{aut}{author}}, {{eaut}{eauthor}} }
答え1
このようなことを行う (内部) クラスがいくつかあります。まず、次のようなコマンドを定義します。
\newcommand\School[1]{\def\@school{#1}}
これにより、「ユーザー」は\School{My wonderful school}
デフォルトを上書きするために使用できるようになります。内部的には、クラスは\@school
学校の名前を印刷する必要があるときはいつでも を使用します。
次に、これが妥当であれば、次のような行をクラスに学校に適切なデフォルトとして設定させます。
\School{My Wonderful School}
授業で。実際、おそらく
\providescommand\@school{My Wonderful School}
ただし、「ヘルパーコマンド」を使用する方が明確だと思います。
私がこれを使用する場所の1つは、これらの変数の多くが他の何かに依存しているチュートリアルシートです。このような場合、私はこれらの変数をすべて1回で設定するためのオプションをクラスに定義します。つまり、「ユーザー」は次のようにファイルを開始します。
\documentclass[somecourse,solutions]{mytutorials}
そしてmytutorials.cls
私の中には
\DeclareOption{somecourse}{
\CourseName{An exciting counrse}
\Semester{Semester 2}
\CourseNumber{Math 987123}
}
\DeclareOption{solutions}{
...
}
すべてのオプションを処理するには、次のようなものが必要です。
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions
article.cls
これにより、ファイルに定義されていないオプションをピギーバックして渡すことができます。
最後に、ユースケースによっては、現在のディレクトリにあるファイルからデフォルトのセットを自動的にロードしたい場合があります。 または を使用すると、\include
ファイル\input
が存在しない場合に問題が発生します。代わりに、クラス ファイル内で を使用できます\@input
。
\@input{defaults.dat}
これにより、ファイル「defaults.dat」が存在する場合はそれがロードされ、存在しない場合は何も行われません。このファイルはそのまま取り込まれるため、ファイルにゴミが含まれているとすべてが壊れてしまうため、このトリックを使用すると危険があります。もちろん、このファイルが存在しないケースをカバーするには、適切なデフォルトを設定する必要があります。
(ちなみに、独自の内部変数については、 を使用することにまったく害はなく\def
、その方が簡単です。もちろん、最初に他のものを上書きしていないことを確認する必要があります!)