我已經使用 LaTeX 一段時間了,但我創建巨集或類別的經驗為零。
基本上我想要做的是創建諸如\subject
、\major
和之類的命令\college
,並且能夠在文章類文檔的標題和頁眉/頁腳中使用這些命令。
我已經能夠「手動」完成工作,如我的 MWE 中所示。
\documentclass[12pt,a4paper,twocolumn]{article}
\usepackage[utf8]{inputenc}
\usepackage[spanish,mexico]{babel}
\usepackage{blindtext}
\title{A title}
\author{ an Author\\{\small a subject}\\\begin{footnotesize}
\textit{a College}
\end{footnotesize}}
\date{\today}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[R]{a Major}
\fancyhead[L]{an Author}
\fancyfoot[C]{Página \textbar \thepage}
\fancyfoot[R]{a Subject}
\fancyfoot[L]{a Title}
\fancypagestyle{plain}{%
\fancyhf{}
\fancyfoot[CE,CO]{Página \textbar \thepage}
\renewcommand{\headrulewidth}{0pt}}
\begin{document}
\maketitle
\blindtext
\blindlist{itemize}[5]
\blindmathpaper
\end{document}
我想讓它變得更加“自動”,只需填充標籤,如下所示:
\major{Mechanical Engineering}
我怎樣才能實現這個目標?
答案1
最簡單的方法是定義一個包含文字的命令並在需要時使用它。
\newcommand\major{Mechanical Engineering}
...
\fancyhead[R]{\major}
定義和使用不必按照此文字順序;\major
只需在使用前定義即可。
如果您想隱藏命令的定義並以類似的方式進行操作\author
,\title
等等,您需要付出一些額外的努力。
\newcommand\major[1]{\newcommand\themajor{#1}}
...
\major{Mechanical Engineering}
...
\fancyhead[R]{\themajor}
\themajor
當before 使用\major
或 if\major
使用兩次時,此程式碼將給出錯誤。在我看來,這就像一個功能,但如果你想避免這種情況,你可以預先定義\themajor
.
\newcommand\themajor{}
\newcommand\major[1]{\renewcommand\themajor{#1}}
...
\major{Mechanical Engineering}
...
\fancyhead[R]{\themajor}
請注意使用\renewcommand
代替\newcommand
。