私はしばらく 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
このコードは、 が の前に使用されたとき\major
、または が 2 回使用されたときにエラーを発生します\major
。 これは機能のように思えますが、これを回避したい場合は、 を事前定義することができます\themajor
。
\newcommand\themajor{}
\newcommand\major[1]{\renewcommand\themajor{#1}}
...
\major{Mechanical Engineering}
...
\fancyhead[R]{\themajor}
\renewcommand
の代わりにを使用することに注意してください\newcommand
。