Let’s Learn LaTex: Part 3

Welcome back to my Let’s Lern LaTex series. I haven’t done any of these posts in a while, but that doesn’t mean i gave up on the idea. Today I want to show you lists which interestingly enough work very much like their corresponding structures in HTML.

For example, LaTex has 3 basic list environments: enumerated list, itemized list and description list. Sounds familiar? It should because these correspond to the ordered list (<ol>), unordered list (<ul>) and definition list (<dl>) in HTML. Let me show you some quick examples – these should be self explanatory.

First let’s do the enumerated list:

\begin{enumerate}
	\item First item
	\item Second item
	\item Third item
\end{enumerate}

As you can see it is a standard environment just like the ones I have shown you before. The only difference is that you preface each item on the list with an \item command. Here is a sample output:

Enumerated list

Itemized list is identical in syntax:

\begin{itemize}
	\item First item
	\item Second item
	\item Third item
\end{itemize}

Here is the output:

Itemized list

Description list is actually even easier to make than in HTML because you don’t need two sets of tags. In the description environment the item command takes an argument which is the word/phrase that is to be used as the word to be defined/described:

\begin{description}
	\item[Foo] Some long description that will probably wrap to the next line, because that's what we want to show. Note how the wrap around works in this case, indenting the second line 
	\item[Bar] And even longer description that will probably wrap to the next line, because that's what we want to show. Actually, this one may not be as long. Whatever.
\end{description}

Note that Foo an Bar are user defined values – you can put whatever you want in there. Here is how it looks once compiled:

Description List

If you want to make lists with multiple indentation levels, you do the same exact thing you would do in HTML: you nest two or three lists like this:

\begin{enumerate}
	\item First item
	\item Second item	
		\begin{enumerate}
			\item First Sub item
				\begin{enumerate}
				    \item First sub sub item
				    \item Second sub sub item
				\end{enumerate}				
			\item Second Sub item
			\item Third Sub item
		\end{enumerate}
	\item Third item
\end{enumerate}

Here is how it would look – note that the counter type changes automatically so that the first level is numbers, second level is characters, third level is roman numerals and etc:

Nested list

First thing people ask after they learn how to use lists is how to change their appearance. For example, how to make enumerated list start with upper case roman numerals instead of numbers. Well, it is not the most straightforward thing to do conceptually. I mean, it can be accomplished in one line, but understanding what is a whole other thing.

You see, in LaTex you can redefine every single command and change what it does. Yep, it is one of those languages where almost nothing is holly and even the most basic operators can be tampered with. This is one of the features that makes LaTex so flexible. To change the way lists work, you basically go in, and you alter the command that LaTex uses internally to display list labels and counters.

By default LaTex recognizes following counter styles:

\arabic	% 1, 2, 3 ...
\alph 	% a, b, c ...
\Alph 	% A, B, C ...
\roman	% i, ii, iii ...
\Roman	% I, II, III ...

You use them like this:

\renewcommand{\labelenumi}{\Roman{enumi}.}

This may seem a bit difficult to grasp but basically in plain english we would say that \renewcommand command redefines the \labelenumi command (which prints out the first level list label) by applying the \Roman style to the first level counter. Does that make sense? If you want to change second and third level, you follow the same pattern:

\renewcommand{\labelenumii}{\roman{enumii}--}
\renewcommand{\labelenumiii}{\{\alph{enumiii}\}}

As you can see, the last argument can be formatted any way you like. You can add dots, parentheses, dashes and etc.. These settings will affect the rest of the document from the point where you first specify them, so if you want to mix and match styles you will need to do this several times in your document. Here is a sample output:

Modified list

Itemized lists can be modified as well using the following command:

\renewcommand{\labelitemi}{\textgreater}

This will change the default bullet to \textgreater which prints out the greater-than symbol like this:

Modified itemized list

There are many symbols you can use as bullets. There is a wide range of them you can find here for example. In general, just google “LaTex Symbols” and you should be all set. If you use a symbol command and you get compilation errors just surround it in $ marks like this:

\renewcommand{\labelitemi}{$\clubsuit$}

This will give you nice club symbols for bullets:

Modified itemized list with math mode

The dollar sign symbols enter LaTex math mode – but you don’t have to worry about that yet. I will talk about it at length in one of the next posts. For now just keep in mind that some symbols are only accessible after you surround them with dollar signs.

I wanted to talk about tables in the same post, but I realized that this post is already getting long so I will stop here. Next week: tables. And maybe something else if we have space.

Let’s Learn LaTex
<< Prev Next >>
This entry was posted in programming and tagged , . Bookmark the permalink.



10 Responses to Let’s Learn LaTex: Part 3

  1. Stefanie BELGIUM Mozilla Ubuntu Linux says:

    Nice work, maybe you can link to this series of posts on your “LaTeX reference page”? That way a new visitor may be more likely to find them.

    About lists, lately I’ve been searching for a way to make LaTeX sort my description lists alphabetically, but I couldn’t find an easy way to do it. The best alternative I could think about was writing a perl script to modify my .tex document before compiling, but I still hope there’s a more elegant solution for this.

    Reply  |  Quote
  2. Hector SPAIN Google Chrome Linux says:

    You have to love how contextual ads work. :-)

    As a result of your posts on this topic I started using latex for documents at work. So please, keep on! ;-p

    Reply  |  Quote
  3. mcai8sh4 UNITED KINGDOM Google Chrome Linux Terminalist says:

    “And even longer description that will probably wrap to the next line, because that’s what we want to show. Actually, this one may not be as long. Whatever.” Haha, sounds like something I would write (although I’d have to include something in parenthesis)

    Stefanie : If you’ve followed some of Lukes other posts and use Vim, you could simply select the range you want to sort, then use the command ‘:sor’ or to remove duplicates ‘:sor u’ (for unique, I think). It’s basic, but does work (for simple lists).

    Another nice post Luke, will there be a Terminally Incoherent LaTeX cheat sheet on the horizon?

    Reply  |  Quote
  4. Alex UNITED STATES Google Chrome Mac OS says:

    A much better explanation of both how and why than I’ve found elsewhere. Can’t wait for the table talk!

    Reply  |  Quote
  5. Scott UNITED STATES Google Chrome Linux says:

    Meant to post last week after your ‘How many lurkers do I have’ post. I’ve been following your blog for over a year (all but the game reviews….just not my thing :) ).

    Love the Latex posts!! You’re making it much more clear than most other references out there! Keep ’em coming!!

    Reply  |  Quote
  6. Luke Maciak UNITED STATES Mozilla Firefox Linux Terminalist says:

    Stefanie wrote:

    Nice work, maybe you can link to this series of posts on your “LaTeX reference page”? That way a new visitor may be more likely to find them.

    Good idea. :)

    Stefanie wrote:

    About lists, lately I’ve been searching for a way to make LaTeX sort my description lists alphabetically, but I couldn’t find an easy way to do it.

    Yeah, that may be difficult. LaTex is not very good at sorting things. Pretty much the only thing you can sort quite readily are your bibliography references. But that actually requires you to run a separate executable, generate bunch of temp files and requires you to run l;atex twice over the source to integrate all of that stuff.

    I will let you know if I find a way to do this. So far I haven’t seen anything that would do that.

    It seems that mcai8sh4’s suggestion below is the simplest thing to do: use a tool to sort the list for you prior to compiling.

    mcai8sh4 wrote:

    Another nice post Luke, will there be a Terminally Incoherent LaTeX cheat sheet on the horizon?

    That is actually a good idea. I think my LaTex reference page was an attempt at something just like that, but I got lazy and never finished it. :)

    Reply  |  Quote
  7. Deltaway UNITED STATES Mozilla Firefox Windows says:

    This series is great! Thanks so much!

    Reply  |  Quote
  8. Rhapsodyine UNITED STATES Mozilla Firefox Windows says:

    Love these. Posted because I’ve just started reading these and running it- you mentioned you may stop these if you didn’t get much feedback, but these are great, you really help making it easy to learn.
    I redid an engineering report I did in Word a couple of weeks ago in LaTeX, and its insane how much better it looks.

    Reply  |  Quote
  9. Jamie CANADA Google Chrome Mac OS says:

    @ Luke Maciak,@ Stefanie:
    Take a look at the datatool package. It allows you to do many database operations in Latex, including sorting lists. Also cool stuff like plots and tables directly from csv files!

    Reply  |  Quote
  10. Chris UNITED STATES Google Chrome Windows says:

    Someone MUST have invented this wheel already in LaTeX, but I can’t seem to figure it out. I’m looking to sort a comma-separated list of items, INLINE, with LaTeX.

    For instance, these data could be Iowa City, Ames, Story City, Des Moines

    In this example, I’d want the section to print the sorted list [of, here, city names]: Ames, Des Moines, Iowa City, Story City

    How might I do this?

    Reply  |  Quote

Leave a Reply

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