From Notes Wiki
Jump to: navigation, search
Home > LaTeX > Creating new LaTeX commands

Creating new LaTeX commands

Syntax of defining command

To create a new LaTeX command which takes one argument one can use something like:

\newcommand{\paragraphtitle}[1]{\paragraph{#1}}

after this if in the LaTeX file one uses \paragraphtitle{ABCD} then LaTeX would automatically convert it to \paragraph{ABCD}. Specifying number of arguments such as [1] is optional. For example one can create command which does not takes any argument as

\newcommand{\bk}{\texttt{\textbackslash}}

It is not necessary to use the argument. One can also create a command as:

\newcommand{\paragraphtitle}[1]{}

which would cause \paragraphtitle{ABCD} to be replaced with nothing .


Defining command at compile time

The advantage of this approach is that the command can be defined on command-line or in Makefile and the output of the LaTeX file will change according to the definition supplied. This can be useful in cases where one needs some things to be displayed (such as paragraph titles) in draft but not in final output. To define a command at compile time one can use:

latex --halt-on-error '\newcommand{\paragraphtitle}[1]{\paragraph{\#1}}\input{research_proposal.tex}'

so that all \paragraphtitle{} in research_proposal.tex file get converted to \paragraph{}. On the other hand if the compilation is done using:

latex --halt-on-error '\newcommand{\paragraphtitle}[1]{}\input{research_proposal.tex}'

then the output will not contain anything specified with \paragraphtitle in file research_proposal.tex.

An example Makefile that uses this technique to generate draft or final version of a research_proposal is:

FILE=research_proposal
latex_cmd1='\newcommand{\paragraphtitle}[1]{\paragraph{\#1}}\input{research_proposal.tex}'
latex_cmd2='\newcommand{\paragraphtitle}[1]{}\input{research_proposal.tex}'
LATEX=latex --halt-on-error

all: draft

draft: ${FILE}.tex
        (${LATEX} ${latex_cmd1}; bibtex ${FILE}; ${LATEX} ${latex_cmd1}; dvipdf ${FILE}.dvi)


final: ${FILE}.tex
        (${LATEX} ${latex_cmd2}; bibtex ${FILE}; ${LATEX} ${latex_cmd2}; dvipdf ${FILE}.dvi)

clean:
        rm -f ${FILE}.dvi
        rm -f ${FILE}.aux
        rm -f ${FILE}.log
        rm -f ${FILE}.out
        rm -f ${FILE}.bbl
        rm -f ${FILE}.blg

clean_all: clean
        rm -f ${FILE}.pdf


Home > LaTeX > Creating new LaTeX commands