LaTeX/C3/newcommand-in-LaTeX/English
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
|
Slide:System Requirements | To record this tutorial, I am using
|
Slide: Pre-requisites | To follow this tutorial,
|
slide: New command
|
We all know that commands are special words that are provided to the compiler.
|
slide: New command | Ensure that the custom command you create is not an already existing LaTeX command.
\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.
| |
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)
\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.
|
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.
|
<<PAUSE>> | |
slide: New command with parameters | Now, we will see newcommand with parameters.
\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)
\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.
\newcommand{\bb}[1]{\mathbb {#1}} before the begin document statement. |
Point to the exact line as per narration | Here \bb is the command.
|
Point to the exact line as per narration | \mathbb {#1} defines the command.
|
Point to the exact line as per narration | Carefully observe the usage after \begin{document}
|
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
|
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)
\newcommand{\add}[2]{\left( #1+#2\right)}
Adding abc and xyz we get $\add{abc}{ xyz}$ \end{document} |
We will copy and past 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}$
|
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.
| |
Let’s now learn about the renewcommand. | |
slide: renewcommand
|
renewcommand is used to change or overwrite the existing command.
\renewcommand{command}{definition}
|
Open new tex file. | Once again, we will open a new tex file. |
(copy/paste the following)
\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.
|
Point to the exact line as per narration | The definition \renewcommand{\S}{\mathcal {S}}
redefines \S.
whereas after redefining \S, produces S in Calligraphic font
|
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.
|
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.
|
Slide: Summary | To summarise, we have learnt
|
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.
Thanks for joining. |