Category Archives: LaTex

Cross-Referencing with LaTeX

Cross-referencing allows you to refer to almost anything in your document automatically through the use of several LaTeX commands. This can become extremely valuable if you have to edit your document and things change. With whatever updates you make the cross-referencing is update automatically.

There are many different ways to cross-reference in LaTeX but we will look at the following.

  • Tables
  • Items in a list
  • Pages

When using cross-referencing you must compile the document twice in order for the numbers to show up. The first time you will only see question marks.

Tables

Below is an example of LaTeX referring to a table.

\documentclass{article}
\begin{document}
   In Table~\ref{Example} we have an example of a table
   \begin{table}[h]
   \begin{center}
   \begin{tabular}{ll}
      \hline
         Fruits&Vegetables\\
      \hline
         Mango&Lettuce\\
         Papaya&Kale\\
      \hline
   \end{tabular}
      \caption{Example} \label{Example}
   \end{center}
   \end{table}
\end{document}

1.png

How a table is created has been discussed previously, what is new here are two pieces of code.

  • ~\ref{  }
  • \label{ }

The “\label” declaration gives the table a label that can be used in the text. In the example, we labeled the table “Example”. The “~\ref” declaration is used in the text and you put the label name on the table inside the curly braces. If you look at the text we never use the number 1 in the text. LaTeX inserts this for us automatically.

The same process can be used to label images as well.

Item in List

Cross-referencing an item on a list is not that complex either. Below is an example.

\documentclass{article}
\begin{document}
   Simple list
   \begin{enumerate}
      \item Mango
      \item Papaya \label{fruit2}
      \item Apple
   \end{enumerate}
   Number \ref{fruit2} is a common fruit in tropical countries.
\end{document}

1.png

As you can see, you can label almost anything anywhere.

Referring to Pages

It is also possible to refer to pages. This can save a lot of time if you update a document and page numbers change. Below is the code and example.

\documentclass{article}
\begin{document}
 Simple list \label{list}
 \begin{enumerate}
    \item Mango
    \item Papaya \label{fruit2}
    \item Apple
 \end{enumerate}
Number \ref{fruit2} in the list on page~\pageref{list} 
is a common fruit in tropical countries.
\end{document}

1.png

We made a label right above our list and then we used the “~\pageref” declaration with the name of the label inside. This provides us with the page number automatically.

Conclusion

There are more complex ways to cross-reference. However, unless you are developing a really complex document they are not really necessary for most practical applications. The ideas presented here will work in most instances as they are.

Advertisements

Insert Images into a LaTeX Document

We have all heard that a picture is worth a thousand words. Images help people to understand concretely what a writer is trying to communicate with text. In this post, we will look at how to include images inside documents prepared with LaTeX.

Basic Example

One way to include an image is to use the “graphicx” package and to set the path for where the image is using the “\garphicspath” declaration in the preamble of a LaTeX document. Below is an example. Included in the example is the “babel” and “blindtext” packages to create some filler text.

\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{graphicx}
\graphicspath{ {PUT THE PATH HERE} }
\begin{document}
\blindtext

\includegraphics[scale=.1]{1.jpg}

\blindtext
\end{document}

1

Inside the actual document we use the following declaration

\includegraphics[scale=.1]{1.jpg}

“\includegraphics” is the declaration. The “scale” argument reduces the size of the image. The information in the curly braces is the name of the actual file. You can see that our print out is rather ugly and needs refinement.

Adding a Caption

One thing our picture needs is a caption that describes what it is. This can be done by first creating a figure environment, placing the “\includegraphics” declaration inside it, and using the “\caption” declration. Below is an example. We will also center the image for aesthetic reasons as well.

\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{graphicx}
\graphicspath{ {PUT THE PATH HERE} }
\begin{document}
\blindtext

\begin{figure}
\centering
\includegraphics[scale=.1]{1.jpg}
\caption{Using Images in \LaTeX}
\end{figure}

\blindtext
\end{document}

1.png

We created a figure environment added our image and type a caption. LaTeX automatically added “Figure 1” to the image. In addition,  you can see that the picture moved to the top of the page. This is because environments are able to float to the best position on a page as determined by calculations made by LaTeX.

If you want the image to appear in a particular place you can add the optional arguments h,t,b,p next to the “\begin{figure}” declaration. h =  here, t = top, b = bottom, and p = separate page.

To get rid of floating use the package called “capt-of”  and the declaration “\captionof{figure or table}{name here}}”. This will freeze the image in place so that it does not move all over the place as you add content to your document. Below is the same example but using the “capt-of” package.

\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{graphicx}
\graphicspath{ {PUT PATH HERE} }
\usepackage{capt-of}
\begin{document}
\blindtext

\begin{center}
\includegraphics[scale=.1]{1.jpg}
\captionof{figure}{Using Images in \LaTeX}
\end{center}

\blindtext
\end{document}

1

This is almost like our first example except now we have a caption. We did have to create a center environment but this type of environment does not float.

Wrapping Figures

The last example is wrapping text around a figure. For this, you need the “wrapfig” package and you need to create an environment with the “Wrapfigure” command. You also must indicate where the figure should be to the left (l),  center (c), or to the right (r). Lastly, you need to indicate the width of the figure. Below is the code followed by the results.

\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{graphicx}
\graphicspath{ {/home/darrin/Downloads/} }
\usepackage{wrapfig}
\begin{document}
\blindtext

\begin{wrapfigure}{r}{7.8cm}
\includegraphics[scale=.1]{1.jpg}
\caption{Using Images in \LaTeX}
\end{wrapfigure}

\blindtext
\end{document}

1.png

In the example above, we moved the image to the left. For the width, you have to guess several times so that all of the text appears next to the figure rather than behind it.

Conclusion

This post provided several practical ways to include images in a LaTeX document. With this amount of control, you are able to make sophisticated documents that are consistently reproduced.

Making Lists in LaTeX

Lists are frequently used in communication in order to provide information succinctly. Often, the rules of grammar can be suspended because of the need for the list to communicate information in an unadorned way. In this post, we will learn how to make lists in LaTeX.

Basic List

For a simple list, you need to make an environment using the “itemize” declaration. Inside this environment, you must use the “\item” declaration for each bullet in the list. Below is the code and printout of a basic list in LaTeX.

\documentclass{article}
\begin{document}
   Shopping list
      \begin{itemize}
         \item lettace
         \item mango
         \item toothpaste
      \end{itemize}
\end{document}

0.jpg

As you can see this is fairly simple. THere is no need for any packages to complete this. If you want a number list instead of creating a “itemize” environment you would create an “enumerate” environment as shown below.

\documentclass{article}
\begin{document}
   Shopping list
   \begin{enumerate}
      \item lettace
      \item mango
      \item toothpaste
   \end{enumerate}
\end{document}

0

Nested List

It is possible to have lists within lists. To do this, you simply create an environment within an environment. LaTeX will automatically change the bullet type for you to enhance readability. Below is an example.

\documentclass{article}
\begin{document}
    Shopping list
    \begin{itemize}
       \item fruits
       \begin{itemize}
          \item lettace
          \item mango
        \end{itemize}
      \item other
      \begin{itemize}
         \item toothpaste
      \end{itemize}
    \end{itemize}
\end{document}

0

The example above has two levels in the list. LaTeX can go up to four levels.

Compact List

Generally, list by default in LaTeX are double-spaced. To reduce this you need to use the “paralist” package with either the “\compactitem” declaration and or the “\compactenum” declaration. Below is the same example but using the paralist features and also blending the use of bullets and numbers.

\documentclass{article}
\usepackage{paralist}
\begin{document}
   Shopping list
      \begin{compactitem}
         \item fruits
         \begin{compactenum}
             \item lettace
             \item mango
         \end{compactenum}
        \item other
        \begin{compactenum}
           \item toothpaste
        \end{compactenum}
      \end{compactitem}
\end{document}

0.jpg

Definition List

It is also possible to make a list of definition. This is useful for a glossary. In order to do this, you create a “description” environment. When you use the “\item” declaration you need to place the definition word in brackets. There are no packages needed for this. Below is the code.

\documentclass{article}
\usepackage{paralist}
\begin{document}
   \begin{description}
      \item[convoluted] complex and hard to understand
      \item [obtuse] slow to understand
   \end{description}
\end{document}

0.jpg

Conclusion

This post provided insights into the use of lists in LaTeX.

Line Breaks and Justification in LaTeX

In this post, we will look at line breaks and justification in LaTeX. These tools will provide a user with more nuanced command of their document.

Paragraph Break

By leaving a space between paragraphs in your document LaTeX will start a new paragraph. Below is the code followed by the output.

\documentclass{article}
\begin{document}
Hello, here is some text without a meaning. This text should show what a printed text will look like at this place. If you read this text, you will get no information.

Really?

Is there no information?

Is there a difference between this text and some nonsense like “Huardest gefburn”?

Kjift – not at all!

A blind text like this gives you information about the selected font.
\end{document}

1.png

Notice how each paragraph is indented. This is the default setting in LaTex. To remove indentation you need to use the “\noindet” declaration as shown below.

\documentclass{article}
\begin{document}
Hello, here is some text without a meaning. This text should show what a printed text will look like at this place. If you read this text, you will get no information.

\noindent

Really?
\noindent

Is there no information?
\noindent

Is there a difference between this text and some nonsense like “Huardest gefburn”?
\noindent

Kjift – not at all!
\noindent

A blind text like this gives you information about the selected font.
\end{document}

1.png

In this example, only the first paragraph is indented.

A simpler way to do this is with the short command line break \\. Below is what it looks like

\documentclass{article}
\begin{document}
Hello, here is some text without a meaning. This text should show what a printed text will look like at this place. If you read this text, you will get no information.\\
Really?\\
Is there no information? \\
Is there a difference between this text and some nonsense like “Huardest gefburn”? \\
Kjift – not at all! \\
A blind text like this gives you information about the selected font.
\end{document}

1

You can see that both “\noindent” and the short command \\ get the same results. However, the latter is probably more efficient and perhaps easier to read.

Justification

There are also ways to remove the default setting for justification. The three declaration are “\raggedright”, “\raggedleft”, and “\centering”. The “\raggedright” declaration makes the right side of the page ragged while the left side of the page is justified as shown below.

\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}
\begin{document}
{\raggedright
\Blindtext}
\end{document}

1.png

You can clearly see how the right side is truly ragged. The other packages in the code create the demo paragraph automatically for us.

The “\raggedleft” declaration does the opposite. See below

\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}
\begin{document}
{\raggedleft
\Blindtext}
\end{document}

1.png

I think we already know what centering does.

\documentclass{article} 
\usepackage[english]{babel} 
\usepackage{blindtext} 
\begin{document} 
{\centering 
\Blindtext} 
\end{document}

1.png

Conclusion

This post provided a demonstration of line breaks and justification in LaTeX.

Modifying Text and Creating Commands in LaTeX

In this post, we are going to explore to separate features available in LaTeX. These two features are modifying the text size and creating custom commands.

Modifying Text

You can change the size and shape of text using many different declarations/environments in LaTeX. Declarations and environments serve the same purpose the difference is in the readability of the code. In the example below, we use an environment to make the text bigger than normal. The code is first followed by the example

\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}
\begin{document}
\begin{huge}
\blindtext
\end{huge}
\end{document}

1.png

Here is what we did.

  1. We create a document with the class of article
  2. We used the “babel” and “blindtext” packages to create some filler text.
  3. Next, we began the document
  4. We create the environment “huge” for enlarging the text.
  5. We used the declaration  “\blindtext” to create the paragraph
  6. We closed the “huge” environment with the “end” declaration
  7. We end the document

If you ran this code you will notice the size of the text is larger than normal. Of course, you can bold and do many more complex things to the text simultaneously. Below is the same example but with the text bold and in italics

\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}
\begin{document}
\begin{huge}
\bfseries
\textit
\blindtext
\end{huge}
\end{document}

1.png

The code is mostly the same with the addition of “\bfseries” for bold and  “\texit” for italics.

Making Commands

It is also possible to make custom commands in LaTeX. This can save a lot of time for repetitive practices. In the example below, we create a command to automatically print the name of this blog’s web address.

\documentclass{article}
\newcommand{\ert}{\bfseries{educationalresearchtechniques}}
\begin{document}
The coolest blog on the web is \ert
\end{document}

1.png

In the code, we use the declaration “\newcommand” in the preamble. This declaration had the command “\ert” which is the shorthand for the code to the right which is “\bfseries{educationalresearchtechniques}. This code tells LaTeX to bold the contents inside the brackets.

The next step was to begin the document. Notice how we used the “\ert” declaration and the entire word educationalresearchtechniques was printed in bold in the actual pdf.

It is also possible to make commands that format text. Below is an example.

\documentclass{article}
\newcommand{\mod}[1]{\textbf{\textit{#1}}}
\begin{document}
The is an example of modified \mod{text}
\end{document}

1.png

What is new is in line 2. Here we use the “\newcommand” declaration again but this time we create a command call “\mode” and give it an argument of 1 (see [1]) this is more important when you have more than one argument. Next, we put in curly brackets what we want to be done to the text. Here we want the text to be bold “\textbf” and in italics “\textit”. Lastly, we set the definition {#1}. Definition works with arguments in that argument 1 uses definition 1, argument 2 uses definition 2, etc.  Having more than one argument and definition can be confusing for beginners so this will not be explored for now.

Conclusion

This post provided assistance in understanding LaTeX’s font size capabilities as well as ways to make new commands.

Basics of LateX

In this post, we will explore more concepts about Latex the typesetting language.

Optional Commands

Optional commands appear in brackets [   ]  when you are using Latex. In the example below, we will set the font size to 20pt in the preamble of the document. The code is as follows.

\documentclass[4paper,12pt]{article} \begin{document} 
     Behold the power of \LaTeX
\end{document}

Here is what it looks like

Screenshot 2018-02-05 14:01:57.png

Inside the brackets, we set the paper size to A4 and the font size to 12pt. Many if not most commands have optional commands that can be used to customize the behavior of the document.

Comments

Like most coding languages Latex allows you to make comments. To do this you need to place a % sign in front of your comment. As shown below

\documentclass[4paper,12pt]{article} \begin{document} 
   Behold the power of \LaTeX
   %This will not print
\end{document}

Screenshot 2018-02-05 14:01:57

Everything after the % did not print. To stop this action simply press enter to move to the next line and you can continue with your document.

Fun with Fonts

There are many different ways to set the fonts. Generally, you can use the \text**{  } code. Where the asterisks are is where you can specify the behavior you want of the text. Below is a simple example of the use of several different formats to the font.

\documentclass[4paper,12pt]{article} 
\begin{document} 
   You can \textit{italicized} 
   Text can be \textsl{slanted} 
   Off course, you can \textbf{bold} text 
   You can also make text in \textsc{small caps} 
   It is also possible to use several commands at the same \textit{\textbf{time}} 
   Behold the power of \LaTeX  
\end{document}

Screenshot 2018-02-05 14:01:57.png

Notice how you put the command in front of the word that you want to format. This might seem cumbersome. However, once you get comfortable with this it is much faster to format documents then the point and click style of Word.

Environments

If you want a certain effect to last for awhile you can use an environment. An environment is a space you declare in your document in which a center behavior takes place. Generally, environments are used to improve the readability of your code. Below is an example.

\documentclass[4paper,12pt]{article} 
    \begin{document} 
        \begin{bfseries} 
            Everything is bold here 
        \end{bfseries} 
        \begin{itshape} 
            Everything is bold here 
        \end{itshape} 
        Behold the power of \LaTeX  
   \end{document}

Screenshot 2018-02-05 14:01:57

An environment always begins with the \begin command and ends with the \end command. In the curly braces, you type whatever is required for your formatting goals. There are scores of commands you can place inside the curly braces.

Conclusion

There is so much more to learn but this is just a beginning. One of the main benefits of learning Latex is the fixed nature of the formatting and the speed at which you can produce content once you are familiar with how to use this language.

Intro to LaTeX

History

LaTex is an open-sourced typesetting document developed about 30 years ago by Leslie Lamport and based on the Tex typesetting of Donald Knuth. It is commonly used in the domains of physics and math for producing mathematical equations and other technical documents. Below is a simple example of an equation developed using LaTex

1

LaTex is a document markup language, which means that you indicate the commands and then it is processed to produce the desired effect. This is in contrast to Microsoft Word which utilizes a WYSIWYG (What you see is what you get) approach.

Benefits

Using LaTex provides several benefits. Cross-referencing is easily accomplish especially with the help of BibTex. It is also multi-lingual and able to make glossaries, indexes, and figures/tables with ease. In addition, LaTex is highly portable and opening a file on any computer is not a problem. Sometimes moving to another computer using Microsoft Word can cause issues with formatting.

Another benefit is psychology, using LaTex allows the author to focus on content and not appearance when writing. It is easy to get distracted when using Word to try and make something work through the point and click mechanism we are so used to when writing.

Cons

It takes extensive time to use LaTex. It looks similar to coding which is intimidating for many. However, once a certain mastery is achieved. Producing documents can be faster as everything is text-based and not point click based using a mouse.

Using LaTex

To use LaTex you need to install TexLive and TexWorks. TextLive is a LaTex distribution and TexWorks is one of many LaTex editors. The editor allows you to manipulate the LaTex code that you generate.

Once you have installed both programs you can type the following into TexWorks. Make sure the typeset is set to pdfLaTex. This allows the output to be a pdf file.

\documentclass{article}
\begin{document}
This is an example of what LaTex does
\end{document}

1.png

What happened is as follows

  1. We entered the command \documentclass{article}. All commands begin with a slash followed by the name. The curly braces are required arguments. In this case, we are using the article template which is one of many templates available in LaTex.
  2. The next command is \begin and this command indicates the beginning of the actual text of the document. Everything above the \begin command is part of what we call the preamble.
  3. Next is the actual text that we want to appear in the pdf.
  4. Lastly, we have the \end command which tells LaTex that the document is finished. Everything between \begin and \end command is part of the environment.

Conclusion

There is so much more that can be accomplished with this typesetting software. The possibilities will be explored in the near future.