LaTeX/C3/newcommand-in-LaTeX/English

From Script | Spoken-Tutorial
Revision as of 15:56, 18 September 2015 by Nancyvarkey (Talk | contribs)

Jump to: navigation, search

Title of script: New Commands

Author: Ambika Vanchinathan

Keywords: New commands, renewcommand, define commands in LaTeX, \newcommand, \renewcommand, video tutorial


Visual Cue Narration
Slide Welcome to the spoken tutorial on creating new commands in LaTeX.
Slide:Learning Objectives This tutorial will help the learners to
  • create or define new commands to get customized output.
  • redefine the existing commands.
Slide: Pre-requisites To follow this tutorial,
  1. You should have knowledge of commands that are necessary to create a document in LaTeX.
  2. You should also have knowledge of compiling and producing a PDF output in LaTeX.
  3. If not, please go through the relevant LaTeX spoken tutorials on this website.
Slide:System Requirements To record this tutorial, I am using
  • Ubuntu Linux 14.04 operating system
  • TeXWorks 0.5
slide: New command


We all know that commands are special words that are provided to the compiler.


Commands start with a backslash and sometimes accept parameters.


Example: \noindent, \textbf, \centering


We can define our own custom commands in LaTeX to save time or for the code to look cleaner.

slide: New command Ensure that the custom command you create is not an already existing LaTeX command.


To define a new command, we write

\newcommand{command}{definition}

at the beginning of the document.

Open the TeXWorks editor Let us open the TeXWorks editor.
Pause the tutorial at this point.


Download and unzip the files provided in the Code Files link below the player.

Switch to the code file You will see some LaTeX code given in the file “newComm.tex” that you downloaded onto your machine.
(copy/paste the following in TeXWorks)


\documentclass{article}

\usepackage{amsfonts}

\newcommand{\bbr}{\mathbb R}

\begin{document}

Let $\bbr$ be a set of Rational numbers.

\end{document}

We will copy and paste the first block of code from this file into our newly opened tex file.
Let’s try to understand the program.
Point to the exact line as per narration The definition

\newcommand{\bbr}{\mathbb R} means that \bbr is the newcommand.


And when used, it will typeset R in the mathbb font.

Point to the exact line as per narration Note that the required packages are loaded in the preamble; in this case \usepackage{amsfonts}
Point to the exact line as per narration So, instead of {\mathbb R}, we can use \bbr within dollars while typesetting.
Save as newComm.tex Let us save the file as newComm.tex
Compile >> Point to the letter R in the pdf output On compilation, we get the pdf output.


Observe the letter R in mathbb font.

<<PAUSE>>
slide: New command with parameters Now, we will see newcommand with parameters.


It is typically defined as -

\newcommand{\command}[parameters]{definition}

open a new file in TeXWorks Let’s open a new file in TeXWorks.
Switch to the code file Now switch to our downloaded code file.
(copy/paste the following)


\documentclass{article}

\usepackage{amsfonts}

\newcommand{\bb}[1]{\mathbb {#1}}

\begin{document}

Let $\bb{R}$ be a set of Rational numbers and $\bb{Z}$ be the set of Complex numbers.

\end{document}

Now, we will copy the next block of code from our downloaded code file and paste it into our new teX file.
Point to the exact line as per narration Let us understand the code now.


We have

\newcommand{\bb}[1]{\mathbb {#1}}

before the begin document statement.

Point to the exact line as per narration Here \bb is the command.


[1] means that the command has one parameter

Point to the exact line as per narration \mathbb {#1} defines the command.


#1 means the first parameter.

Point to the exact line as per narration Carefully observe the usage after \begin{document}


We use it within dollars because it contains math symbols.

Point to the exact line as per narration \bb followed by the open brace and parameter and again we close the brace.
Point to the exact line as per narration We have used it twice with R as the parameter in the first usage


and Z as the parameter in the second usage.

Save as newCommP1.tex

Compile

Save the program as newCommP1.tex and compile it.
And point to the pdf output In the output, observe that the R and Z are in the mathbb or the blackboard font.
<<PAUSE>>
Next, let’s define a newcommand with 2 parameters.
Open a new tex file. Open a new tex file.
(copy/paste the following)


\documentclass{article}

\newcommand{\add}[2]{\left( #1+#2\right)}


\begin{document}

Adding abc and xyz we get $\add{abc}{ xyz}$

\end{document}

We will copy and paste the next block of code from our downloaded file, as shown here.
Point to the exact line as per narration Observe the line

\newcommand{\add}[2]{ \left( #1 + #2 \right) }

in the document.

Point to the exact line as per narration This has been used as $\add{abc}{xyz}$


Here in the definition, \add is the newcommand.

Point to the exact line as per narration [2] indicates that it has 2 parameters.
Point to the exact line as per narration {\left( #1+#2\right)} is the definition of the command.
Point to the exact line as per narration #1 for the first parameter and #2 for the second parameter.
Point to the exact line as per narration #1 and #2 are replaced by the actual text {abc} and {xyz} in this case.
Save as newCommP2.tex

Compile

Let us save as newCommP2.tex and compile the tex file.
Observe the output.


Here the left and right parenthesis are added and a plus symbol is introduced between the first and second parameters.


<<PAUSE>>

Let’s now learn about the renewcommand.
slide: renewcommand


renewcommand is used to change or overwrite the existing command.


The syntax for renewcommand is

\renewcommand{command}{definition}


It’s quite similar to newcommand.

Open new tex file. Once again, we will open a new tex file.
(copy/paste the following)


\documentclass{article}

\usepackage{amsmath}

\renewcommand{\S}{\mathcal {S}}

\begin{document}

Let $\S$ be a set.

\end{document}

We will copy and paste the last block of code from our downloaded file into our newly opened tex file.


Let us understand the program now.



Point to the exact line as per narration The definition \renewcommand{\S}{\mathcal {S}}

redefines \S.


\S normally produces the section symbol (§)

whereas after redefining \S, produces S in Calligraphic font


Note that the required package amsmath is preloaded.


Also, we use \S within dollar symbols.

Save as renew.tex

Compile

Point to the “S” in the PDF output.

Let us now save the file as renew.tex, compile it and see the output.


We can see “S” in calligraphic font.

Come back to the tex file. Come back to the tex file.
Replace \z for \S, compile and point out the error. Note that replacing z for S in the renewcommand does not work.


This is because there is no existing command defined as \z.


Please use renewcommand only when you are very sure about what you are doing.

Slide: Summary To summarise, we have learnt
  • newcommand
  • newcommand with parameters
  • renewcommand
slide: Assignment


Create a new command \textbfit which will convert the given text into bold and italics.
Slide: Acknowledgement The video at the following link summarises the Spoken Tutorial project.

Pls watch it.

Slide The Spoken Tutorial Project Team :

Conducts workshops using spoken tutorials and

Gives certificates to those who pass an online test

For more details, please write to us.

Slide Spoken Tutorial Project is funded by the NMEICT, MHRD, Government of India.


More information on this Mission is available at[1]this[2]link.


This is Ambika Vanchinathan signing off.

Thanks for joining.

Contributors and Content Editors

Ambika, Nancyvarkey