1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

LaTeX の \newcommand を \newcommand してみる

Last updated at Posted at 2020-11-02

#\newcommand とは
LaTeXでは,\newcommandを使ってマクロを定義することができます.

\newcommand{<new command>}[<option>]{<definition>}

optiondefinitionの中で取る引数の数です.例えばこんな感じです.

\newcommand{\f}[1]{\fbox{(#1)}} %穴埋め式問題の問題文の四角

すると\f{ア}と打った時,

\fbox{(ア)}

と表示されるようになります.
他にも
\renewcommand(すでに存在するコマンドの旧定義を上書き),
\providecommand(定義しようとしているコマンドがすでに存在する場合何もせず,未定義の場合新しく定義),
\newtheorem(定理用の環境を定義),
\newenvironment(新しい環境を定義)
などがあります.

#問題点
これらのコマンドは結構長い.作業中何か思いついたときに\newcommandと打つのって結構長いですよね.機械的な部分にはなるべく労力を割かずに,創造的な箇所にエネルギーを使いたいものです.
エディタは Atom を使っていますが自動補完もしてくれないし,手で自動補完の設定を加えたとしてもそれはエディタに依存してしまいます.なるべく他人も使えるように,また自分のファイルを誰かに渡したらそれでその人も助かるような,そんな解決策はないものでしょうか.

\newcommand を \newcommand する

そこで\newcommand\newcommandしちゃいましょう.必要は発明の母です.やってみると実に簡単です,プリアンブルに以下のように書くだけです.

newcommand.sty
\newcommand{\nc}{\newcommand} %doesn't mean noise cancelling!
\nc{\rnc}{\renewcommand}
\nc{\pvc}{\providecommand}
\nc{\nthm}{\newtheorem}
\nc{\nenv}{\newenvironment}
%\pvc{A}{B}: Only if A doesn't exists, A is defined as B.

元々コマンドが存在するのを省略するだけの場合は,引数の指定は必要ないようです.これで無事\newcommandを打つのがめんどくさいという問題は解決しました.

ちなみに\rncを使うと元の定義は使えなくなってしまうので,僕は特別の理由がない限り極力\ncの方を使うようにしています.

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?