LaTeX Unlocked: A Powerful Blueprint for Beginners to Conquer Document Design! (Part 2)

Your Web Page LaTeX - Screenshot of LaTeX document

Table of Contents

Introduction

In the first part of this blog, we covered the fundamentals of LaTeX, from installation and basic structure to document formatting and typography. Building on this foundation, the second section of our investigation delves into more advanced LaTeX features to help you improve your document preparation skills. In this section, we’ll go over how to define custom commands and environments, how to incorporate graphics seamlessly into your documents, how to master the complexities of figures and floating bodies, and how to manage content alignment within columns. In addition, we will look at tools that are essential for creating well-structured and dynamic documents, such as table of contents, cross-referencing, and bibliography.  The journey continues with discussions on creating indices and glossaries, preparing presentations with LaTeX Beamer, using the letter class to format letters, and customizing letterhead and signatures. 

Defining Custom Commands and Environments in LaTeX

Welcome to the world of LaTeX customization! This chapter will cover the strong tools of defining environments and commands, which will enable you to create reusable formatting magic blocks and organize your writing more efficiently. Consider it as creating a customized mini-LaTeX toolbox based on your requirements.

Why Define Commands?

Consider entering the same complex phrase or formula again and over in your paper. Isn’t it tedious? Commands come to our aid! Create a command with the necessary formatting once, and then call it wherever you need it. This saves time, reduces typos, and keeps the code in your page clear and succinct.

Defining Commands:

In LaTeX, defining a new command involves using the \newcommand or \renewcommand macro. The basic syntax for defining a new command is as follows:

				
					\newcommand{\commandname}[number_of_arguments][default_value]{definition}

				
			

Let’s break down each component:

  • \newcommand:  This is the command used to create a new command.
  • {}:  The curly braces enclose the name of the new command, for example, \commandname.
  • [number_of_arguments]:  This optional parameter specifies the number of arguments the new command will take. If the command doesn’t take any arguments, this part can be omitted.
  • [default_value]:  This optional parameter is used only when specifying the number of arguments. It allows you to set default values for the arguments.
  • {definition}:  This is the actual definition of the command. It specifies what the command will do when invoked. It can include formatting instructions, text, or other commands.

Let’s start with a simple example. Say you frequently type a 3D vector using ‘bmatrix’ environment.   Instead of typing it out every time, you can define a custom command.  Here is an example: 

				
					\documentclass{article}
\usepackage{amsmath}  % Required for the bmatrix environment

% Define a custom command for displaying a 3D vector
\newcommand{\vectorxyz}[3]{\begin{bmatrix} #1 \\ #2 \\ #3 \end{bmatrix}}

\begin{document}

\section{3D Vector Example}

Consider the 3D vector $\mathbf{V}$ and $\mathbf{W}$ defined as:\quad $ \mathbf{V} = \vectorxyz{2}{-1}{3} $ and $ \mathbf{W} = \vectorxyz{0}{-4}{1} $
\end{document}

				
			

In this example:

  • \newcommand{\vectorxyz}[3]:  This line defines a new command named \vectorxyz that takes three arguments enclosed in curly braces {}.
  • \begin{bmatrix} #1 \\ #2 \\ #3 \end{bmatrix}:  Within the command definition, we use the bmatrix environment from the amsmath package to create a column vector with three rows. The #1, #2, and #3 are placeholders for the three arguments passed to the command.

To use this command, simply call \vectorxyz{2}{-1}{3} (or any other values you prefer) within your document. This will produce a 3D vector enclosed in square brackets:

Embedded PDF

Example 2: Let’s create a command for displaying a 3×3 matrix using the 'pmatrix' environment.

				
					\documentclass{article}
\usepackage{amsmath}  % Required for the pmatrix environment

% Define a custom command for displaying a 3x3 matrix
\newcommand{\matrixxyz}[9]{\begin{pmatrix} #1 & #2 & #3 \\ #4 & #5 & #6 \\ #7 & #8 & #9 \end{pmatrix}}

\begin{document}

\section{3x3 Matrix Example}

Consider the 3x3 matrices $\mathbf{M}$ and $ \mathbf{N}$ defined as:

\[
\mathbf{M} = \matrixxyz{1}{2}{3}{4}{5}{6}{7}{8}{9} \quad \mathbf{N} = \matrixxyz{1}{0}{3}{-4}{5}{-7}{0}{8}{0}
\]

\end{document}

				
			

In this example:

  • \newcommand{\matrixxyz}[9]: This line defines a new command named \matrixxyz that takes nine arguments enclosed in curly braces {}.
  • \begin{pmatrix} #1 & #2 & #3 \\ #4 & #5 & #6 \\ #7 & #8 & #9 \end{pmatrix}: Within the command definition, we use the 'pmatrix' environment to create a 3×3 matrix. The #1 to #9 are placeholders for the nine arguments passed to the command.

To use this command, call \matrixxyz{1}{2}{3}{4}{5}{6}{7}{8}{9} (or any other values you prefer) within your document. This will produce a 3×3 matrix:

Embedded PDF

Example 3: Let’s create a command for the quadratic formula, which is a commonly used formula to find the roots of a quadratic equation. The quadratic formula for an equation \( ax^2+bx+c=0 \) is given by \( x=\frac{-b \pm \sqrt{b^2-4ac}}{2a} \).  Here’s how you can define a command for the quadratic formula:

				
					\documentclass{article}
\usepackage{amsmath}  % Required for mathematical symbols like \sqrt

% Define a custom command for the quadratic formula
\newcommand{\quadraticformula}[3]{\frac{-(#2) \pm \sqrt{(#2)^2 - 4(#1)(#3)}}{2(#1)}}

\begin{document}

\section{Quadratic Formula Example}

Consider the quadratic equation \(2x^2 - 5x + 3 = 0\). The roots of the equation can be found using the quadratic formula:

\[ x = \quadraticformula{2}{-5}{3} \]

\end{document}

				
			

In this example:

  • \newcommand{\quadraticformula}[3]:  This line defines a new command named ‘\quadraticformula' that takes three arguments enclosed in curly braces {}.
  • \frac{-(#2) \pm \sqrt{(#2)^2 - 4(#1)(#3)}}{2(#1)}:  Within the command definition, we use \pm to represent both roots of the quadratic equation, and #1, #2, and #3 are placeholders for the three coefficients passed to the command.

To use this command, call \quadraticformula{2}{-5}{3} (or any other coefficients you prefer) within your document. This will produce the roots of the quadratic equation.

Embedded PDF

Custom Environment

A custom environment in LaTeX is a powerful tool for defining your own structured blocks of content with specific formatting or behavior. Custom environments are especially useful when you want to reuse a consistent pattern or structure throughout your document. They are defined with the '\newenvironment' command and can be used to make it easier to include complex structures.

Defining a Custom Environment:

To define a custom environment, you use the ‘\newenvironment' command. The basic syntax is as follows:

				
					\newenvironment{environmentname}[num]{before}{after}

				
			
  • environmentname:  This is the name of your custom environment.
  • num:  This is an optional parameter specifying the number of arguments the environment takes (similar to custom commands).
  • before:  This is the code that will be executed before the content of the environment.
  • after:  This is the code that will be executed after the content of the environment.

Example 1: Let’s create a custom environment for typesetting theorems. Theorems often have a consistent structure with a title and content. Here’s an example that defines a custom environment named ‘mytheorem':

				
					\documentclass{article}
\usepackage{xcolor}  % Required for text color

% Define a custom environment for theorems
\newenvironment{mytheorem}[1]{
    \par\medskip\noindent
    \textbf{\textcolor{blue}{Theorem: #1}}\quad
    \begin{itshape}
}{
    \end{itshape}
}

\begin{document}

\section{Theorem Example}

Let's use our custom theorem environment:

\begin{mytheorem}{Pythagorean Theorem}
In a right-angled triangle, the square of the length of the hypotenuse ($c$) is equal to the sum of the squares of the lengths of the other two sides ($a$ and $b$).
\[ c^2 = a^2 + b^2 \]
\end{mytheorem}

\begin{mytheorem}{Quadratic Formula}
For a quadratic equation $ax^2 + bx + c = 0$, the solutions are given by:
\[ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \]
\end{mytheorem}

\end{document}
				
			

In this example:

  • \newenvironment{mytheorem}[1]{...}{...}:  This defines a new environment named mytheorem that takes one argument (the theorem title).
  • \par\medskip\noindent:  Starts a new paragraph with some vertical space and no indentation.
  • \textbf{\textcolor{blue}{Theorem: #1}}\quad:  Prints the theorem title in bold, blue text.
  • \begin{itshape} ... \end{itshape}:  The content of the theorem is set in italics.

To use this custom theorem environment, you simply need to call it using \begin{mytheorem}{Title} and end it with \end{mytheorem}, providing the title and content of the theorem as arguments.

Embedded PDF

Example 2:  Let’s create a custom environment named myquotebox for writing a quote in italics and contained in a box. We’ll use the ‘mdframed' package to create the box.

				
					\documentclass{article}
\usepackage{mdframed}  % Required for creating framed boxes
\usepackage{xcolor}  % Required for color customization

% Define a custom environment for quotes in a box
\newenvironment{myquotebox}{
    \begin{mdframed}[
        backgroundcolor=blue!10!white,
        linecolor=blue!30!white,
        innerleftmargin=10,
        innerrightmargin=10,
        innertopmargin=5,
        innerbottommargin=5,
        roundcorner=10pt,
        linewidth=1pt  % Adjust the linewidth for the outline
    ]\itshape%
}{
    \end{mdframed}
}

\begin{document}

\section{Quote Examples}

Let's use our custom quote box environment:

\begin{myquotebox}
"The only limit to our realization of tomorrow will be our doubts of today." \\[2ex]
\textbf{- Franklin D. Roosevelt}
\end{myquotebox}

\begin{myquotebox}
"Success is not final, failure is not fatal: It is the courage to continue that counts." \\[2ex]
\textbf{- Winston Churchill}
\end{myquotebox}

\end{document}

				
			

In this example:

  • \newenvironment{myquotebox}{...}{...}:  This defines a new environment named myquotebox.
  • \begin{mdframed}[...]:  Starts a framed box using the mdframed package. We customize the appearance of the box using various options like background color, margins, and rounded corners.
  • \itshape:  Switches the font to italics.
  • {...}:  This is where you place the content of your quote.
  • \end{mdframed}:  Ends the framed box.

To use this custom myquotebox environment, you can simply call it using \begin{myquotebox} and end it with \end{myquotebox}, placing your quote content in between.

Embedded PDF

Example 3: Let’s create a simple custom environment named 'simplebox' that adds a colored background to a block of text. We’ll use the ‘tcolorbox' package for this purpose.

				
					\documentclass{article}
\usepackage{xcolor} % Required for color definitions
\usepackage{tcolorbox} % Required for creating colored boxes

% Define colors
\definecolor{lightblue}{RGB}{173,216,230}

% Define a new environment named 'simplebox' for a colored block of text
\newenvironment{simplebox}{
  \begin{tcolorbox}[
    colback=lightblue, % Background color (light blue)
    colframe=white, % Frame color (white)
    sharp corners=all, % Sharp corners
    boxrule=0pt % No border
  ]
}{
  \end{tcolorbox}
}

\begin{document}

% Use the custom 'simplebox' environment for a simple colored block of text
\begin{simplebox}
This is a simple example of a colored box in \LaTeX.
\end{simplebox}

\begin{simplebox}
The solutions of the quadratic equation \( ax^2+bx+c=0\) are given by \[ x=\frac{-b \pm \sqrt{b^2-4ac}}{2a}\]
\end{simplebox}

\end{document}

				
			
Embedded PDF

Including Graphics in LaTeX

LaTeX excels at incorporating graphics seamlessly, allowing users to incorporate images, diagrams, and figures into their documents. In this section, we’ll look at how to include graphics in LaTeX, including syntax, examples, and customization options to make the process easier for beginners.

Introduction to Graphics in LaTeX

Before diving deeper into the syntax and examples, it is important to understand the basics of including graphics in LaTeX. The most commonly used package for this purpose is the ‘graphicx’ package, which provides a set of commands to manage images. To use this package, include the following line in the introduction of your LaTeX document:

				
					\usepackage{graphicx}

				
			

With this package loaded, you can begin incorporating graphics into your document.

Where to Save Graphics Files

  • If you are using an offline LaTeX editor, it is important to know where to save your graphics files. Generally, it is recommended to save your images in the same directory as your LaTeX document. This ensures that LaTeX can easily detect and include images. However, you can also specify a different path if necessary.
  • For Overleaf, a popular online LaTeX editor, the process is more straightforward. You can upload your graphics files directly to your Overleaf project, and the platform will handle the rest.

Basic Syntax for Including Graphics

The fundamental command for including graphics in LaTeX is '\includegraphics'. The syntax is straightforward:

				
					\includegraphics[options]{filename}

				
			

Here, 'filename' refers to the name of the image file you want to include, and 'options' allow you to customize the appearance of the image. Let’s explore some commonly used options:

a. Scale Option

You can use the 'scale' option to adjust the size of the image relative to its original dimensions. For example:

				
					\includegraphics[scale=0.5]{example-image}

				
			

This will include the image scaled to 50% of its original size.

b. Width Option

To specify the width of the image, you can use the 'width' option:

				
					\includegraphics[width=5cm]{example-image}

				
			

This will set the width of the image to 5 centimeters.

c. Height Option

Similarly, you can set the height of the image using the 'height' option:

				
					\includegraphics[height=3in]{example-image}

				
			

This will set the height of the image to 3 inches.

d. Angle Option

To rotate an image, you can use the 'angle' option:

				
					\includegraphics[angle=90]{example-image}

				
			

This will rotate the image by 90 degrees.

Positioning Graphics Within the Document

Controlling graphic placement in a LaTeX document is critical for achieving a professional and polished appearance. This section goes over two popular approaches: floating figures and fixed placement.

1. Floating Figures

LaTeX includes a  'figure' environment that allows you to create floating figures that move dynamically inside the document based on available space. This is very useful when working with large documents or when you want LaTeX to manage the positioning.

				
					\begin{figure}[htbp]
    \centering
    \includegraphics[width=0.8\linewidth]{example.png}
    \caption{An example of a floating figure.}
    \label{fig:float_example}
\end{figure}

				
			

Here’s a step-by-step explanation:

  1. \begin{figure}[htbp]:  This command begins the figure environment. The optional argument [htbp] provides placement options for the figure. It specifies that LaTeX should try to place the figure here (h), at the top of the page (t), at the bottom of the page (b), or on a separate page (p), in that order of preference.

  2. \centering:  This command centers the content inside the figure horizontally.

  3. \includegraphics[width=0.8\linewidth]{example.png}:  This command includes the graphic file named “example.png” in the figure. The optional argument [width=0.8\linewidth] specifies that the width of the included graphic should be 80% of the current line width. You can adjust this percentage based on your preferences.

  4. \caption{An example of a floating figure.}:  This command adds a caption to the figure. The text within the curly braces is the caption itself, which in this case is “An example of a floating figure.”

  5. \label{fig:float_example}:  This command labels the figure with a reference name. The label can be used to reference the figure elsewhere in the document. In this case, the label is “fig:float_example.”

  6. \end{figure}:  This command ends the figure environment.

Consider the following two examples:

				
					\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}

\begin{document}

\section{Surface of Revolution Example}

In this example, we will visualize the surface of revolution generated by revolving the function $y = \sqrt{x}$ around the x-axis.

\begin{figure}[h]
    \centering
    \includegraphics[width=0.8\linewidth]{surfaceofrevolution.png}
    \caption{Surface of revolution generated by $y = \sqrt{x}$}
    \label{fig:surface_of_revolution}
\end{figure}

As shown in Figure \ref{fig:surface_of_revolution}, the three-dimensional surface is formed by revolving the curve $y = \sqrt{x}$ around the x-axis.

\end{document}

				
			
Embedded PDF
				
					\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}

\begin{document}

\section{Surface of Revolution Example}

In this example, we will visualize the surface of revolution generated by revolving the function $y = \sqrt{x}$ around the x-axis.

\begin{figure}[t]
    \centering
    \includegraphics[width=0.8\linewidth]{surfaceofrevolution.png}
    \caption{Surface of revolution generated by $y = \sqrt{x}$}
    \label{fig:surface_of_revolution}
\end{figure}

As shown in Figure \ref{fig:surface_of_revolution}, the three-dimensional surface is formed by revolving the curve $y = \sqrt{x}$ around the x-axis.

\end{document}

				
			
Embedded PDF

The primary difference between the above two examples lies in the optional argument provided for the placement of the figure within the document.  In the first example, the optional argument [h] is used. This suggests to LaTeX that the figure should be placed “here,” meaning approximately at the same location in the document where the figure is defined in the code.  In the second example, the optional argument [t] is used. This suggests to LaTeX that the figure should be placed at the “top” of the page.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top