LaTeX/C3/Writing-Style-Files-in-LaTeX/English
Title of script: Writing style file in LaTeX
Author: Arun Rajput
Keywords: style, package, custom, commands, newcommand, renewcommand, environment, newenvironment, renewenvironment, redefine, ubuntu, texworks, LaTeX
| Visual Cue | Narration |
| Slide: Title | Hello and welcome to the spoken tutorial on “Writing Style files” in LaTeX. |
| Slide: Learning Objectives | In this tutorial we will learn how to:
|
| Slide: System Requirements | This tutorial is recorded on
The steps demonstrated will work in all operating systems. |
| Slide: Pre-requisites |
|
| Slide: Code Files |
|
| Slide: Style File |
|
| Slide: Style File contd. |
|
| Slide: Style File contd. |
|
| Not on slide, only narration from script | We usually define our own style file in LaTeX to save time and get cleaner code. |
| Slide: Style File |
|
| Go to Downloads folder >> Stylefile-Code-Files | Go to the folder where all the files are downloaded. |
| Double-click the file myPackage.sty | Open the file myPackage.sty in TeXworks editor.
We will see some LaTeX code written in the file. |
| Let us try to understand the code. | |
| Highlight
\newcommand{\iitb}{\textbf{Indian Institute of Technology – Bombay}} |
This newcommand definition means that \iitb is the newcommand.
Whenever \iitb is used in a LaTeX document, it will typeset the full name of IITB in bold font. |
| Highlight \RequirePackage command | \RequirePackage command is used to load the required packages in the style files. |
| Highlight \RequirePackage{background} | We are using \RequirePackage{background} for setting the background image. |
| Highlight \newcommand{\iitbbg} | I have defined \newcommand{\iitbbg} to load IITB’s logo as the background image. |
| Highlight \iitbbg | So every time instead of loading the image manually, we can use \iitbbg command. |
| We have now successfully created our custom style file.
Next let us see how to use this style file. Please note that we do not compile .sty files. | |
| Close the file. | Let’s close this file. |
| Open the file custom.tex in TeXWorks. | Now open the file custom.tex in TeXworks editor. |
| Highlight
\usepackage{myPackage} |
The new style file is loaded in the preamble using \usepackage command.
In our example, we have loaded our style filemyPackage. |
| Highlight \iitb \iitbbg | After loading the style file, we use\iitb and \iitbbg commands inside the document section. |
| At the top left drop-down >> select pdfLaTeX | At the top left, in the drop-down, select pdfLaTeX if not already selected. |
| Click on the green arrow button | Then click on the green arrow button to compile the file. |
| Cursor onthe pdf output on the right | On compilation, we get the pdf output on the right.
Observe the full name of IITB in the PDF. |
| Switch to custom.tex | Come back to custom.tex file.
Here, we had included the command \iitbbg to display the background image. |
| Highlight the pdf output once | But we’re not seeing that in the output. |
|
Click on the green arrow button |
To display the background image, we may need to compile the code once again.
Let us compile it again. |
| Circle the cursor on the pdf output once | And here is the background image! |
| Sometimes, we have to compile the LaTeX code twice, if we don’t see the expected output. | |
| Slide: Defining standard style file | Now let’s modify our style file into a standard style file.
The reason is, in the real-world you would want to share your style file with others. Ideally, it must adhere to the style file guidelines given by the LaTeX community. |
| Slide: Defining standard style file contd. | The structure of the style file can be summarized into 3 parts:
|
| Slide: Defining standard style file contd. | More information on this guideline is available at this link
|
| Go to the folder Stylefile-Code-Files | Go to the folder where the code files are saved. |
| [TeXworks] myStdPackage.sty | Let’s open the file myStdPackage.sty in TeXworks. |
| Highlight the definition | We have written some definition inside our style file.
Let’s try to understand the code. |
| Highlight
NeedsTeXFormat{} |
NeedsTeXFormat{} is used to specify the version of LaTeX in which this style file will work.
In our example, it is LaTeX2e. |
| Highlight
\ProvidesPackage{myStdPackage}[2019/03/ 11 v0.01 LaTeX package for my own purpose] Highlight the filename in the titlebar |
ProvidesPackage is used for identification of the package.
The package name should be identical to the filename. Here I am using myStdPackage as a parameter and file name. We willalso provide other details like release date and description of the package. |
| Highlight.
\newcommand{\iitb}{\textbf{Indian Institute of Technology - Bombay}} \newcommand{\iitbbg}{\backgroundsetup{scale = 1, angle = 0, opacity = 0.1, contents = {\includegraphics[width = \paperwidth, height = \paperheight, keepaspectratio] {iitblogo.pdf}}}} |
From here onwards, we define our commands and definitions.
Recall, we have used this code in myPackage.sty file. |
| Highlight
\endinput |
\endinput must be the last command in this file.
It indicates that the style file ends here. |
| Only narration | Now we have modified our style file into a standard style file. |
| Switch to custom.tex. | Next let’s switch back to the LaTeX file custom.tex. |
| Highlight \usepackage{myStdPackage} | In the usepackage command change the package name to myStdPackage. |
| Ctrl+S keys >> Click on the green arrow button | Then save and compile the file. |
| Point to the PDF output | We get the same pdf output without any error.
This means our new standard stylepackage is working correctly. |
| Slide: Sharing .sty with Community | Once we have created our own style file, we can share this with the LaTeX community too.
|
| This brings us to the end of this tutorial.
Let us summarise. | |
| Slide: Summary | In this tutorial we learnt
|
| slide: Assignment | As an assignment:
|
| Slide: About Spoken Tutorial project | The video at the following link summarises the Spoken Tutorial project.
Please watch it. |
| Slide: Spoken Tutorial workshops | The Spoken Tutorial Project Team conducts workshops and gives certificates.
For more details, please write to us. |
| Slide: Forum | Please post your timed queries in this forum |
| Slide: Acknowledgement | Spoken Tutorial Project is funded by MHRD, Government of India.
More information on this Mission is available at this link. |
| Slide: Thanks Slide | The script is contributed by Arun, FOSSEE Summer Fellow 2019
And this is Praveen from IIT Bombay signing off. Thanks for joining. |