LaTeX/C3/Writing-Style-Files-in-LaTeX/English

From Script | Spoken-Tutorial
Revision as of 11:02, 17 December 2019 by Pravin1389 (Talk | contribs)

Jump to: navigation, search

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:
  • Write a custom style file
  • Import a custom style file in a LaTeX document and
  • Define a standard style file
Slide: System Requirements This tutorial is recorded on
  • Ubuntu Linux 18.04 OS and
  • TeXworks Editor v0.6.

The steps demonstrated will work in all operating systems.

Slide: Pre-requisites
  • To practise this tutorial, you should be familiar with LaTeX commands.
  • You should also know how to define newcommand and newenvironment in LaTeX.
  • If not, then go through the prerequisite LaTeX spoken tutorials on this website.
Slide: Code Files
  • The files used in this tutorial are available in the Code Files link on this tutorial page.
  • Please download and use them while practising.
  • Make sure that all the supporting files are kept in the same folder while practising.
Slide: Style File
  • Style files are also known as Packages.
  • These are supplementary files that increase the functionality of LaTeX.
Slide: Style File contd.
  • You might have used various packages in your LaTeX document earlier.
  • For example, we import geometry package for defining margin.
Slide: Style File contd.
  • To reuse the same code many times in the same document, we use newcommand and newenvironment.
  • To reuse the same code many times in many documents, we use style files.
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
  • The tex file containing the content will remain clean, if the styles are in separate sty files.
  • Hence, it is advised to write and use style files.
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 definitionmeans 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 timeinstead 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:
  • Identification, includes package name and version
  • Preliminary declarations, includes details of the required package and
  • User code.
Slide: Defining standard style file contd. More information on this guideline is available at this link

https://en.wikibooks.org/wiki/LaTeX/Creating_Packages

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 insideour 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 exampleit 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 usingmyStdPackage 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 thestyle 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 tothe 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 andcompile 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.
  • Upload the created style file on the Comprehensive TeX Archive Network
  • Provide proper documentation and read the guidelines before uploading.
This brings us to the end of this tutorial.

Let us summarise.

Slide: Summary In this tutorial we learnt
  • Write a custom style file
  • Import the custom style file in a LaTeX document
  • Define a standard style file
slide: Assignment As an assignment:
  • Identify and make a list of different scenarios where creating a style file would be helpful.
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.

Contributors and Content Editors

Nancyvarkey, Pravin1389