GeneralAdding icons to left of enumerate items

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
dmt
Posts: 82
Joined: Sat Mar 15, 2008 8:13 pm

Adding icons to left of enumerate items

Post by dmt »

If I have an enumerated list, how can I add an icon or symbol to the left of individual items in the list (not all of them)?

For example, if I wanted to have one happy face to the left of item 4 and then two happy faces to the left of item 8?

Thanks.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Adding icons to left of enumerate items

Post by Juanjo »

Code: Select all

\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{wasysym}
\usepackage{url}

\begin{document}
\newcommand{\IconCry}{\includegraphics[width=1em]{icon_cry.jpg}}
\newcommand{\IconRazz}{\includegraphics[width=1em]{icon_razz.jpg}}
\newcommand{\IconEWink}{\includegraphics[width=1em]{icon_e_wink.jpg}}

\begin{itemize}
   \item[\IconCry] An item of a list
   \item Other item, this one uses the default label
   \item[\IconRazz] A smiley
   \item Again the default label
   \item[\IconEWink] Is it clear now?
   \item[\frownie] This face comes from the \textsf{wasysym} package
   \item[\smiley] This one too.
\end{itemize}
This also works with \texttt{enumerate} instead of \texttt{itemize}.

Check this document for a large list of predefined symbols:\\ \small
\url{www.ctan.org/tex-archive/info/ symbols/comprehensive/symbols-a4.pdf}

\end{document}
O.K.? ;)
Attachments
icon_e_wink.jpg
icon_e_wink.jpg (1.75 KiB) Viewed 14288 times
icon_razz.jpg
icon_razz.jpg (1.07 KiB) Viewed 14289 times
icon_cry.jpg
icon_cry.jpg (1.74 KiB) Viewed 14289 times
dmt
Posts: 82
Joined: Sat Mar 15, 2008 8:13 pm

Re: Adding icons to left of enumerate items

Post by dmt »

Thanks, but I think I wasn't clear enough.

I want the normal numbering in an enumerate.

1.

2.

3.

etc.

But to the left of some of the numbers I would like to sometimes put a symbol. For example:

1.
2.
*3.
4.
**5.

etc.

with the numbers lining up like normal over each other and the symbols appearing in the margin to the left.

I want to use this for example to label certain homework problems I am creating as harder or easier, while keeping the same numbering scheme.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Adding icons to left of enumerate items

Post by Juanjo »

Let's try again:

Code: Select all

\documentclass[12pt]{article}
\usepackage{graphicx}

\newcommand{\IconCry}{\includegraphics[width=1em]{icon_cry.jpg}}
\newcommand{\IconRazz}{\includegraphics[width=1em]{icon_razz.jpg}}

\newlength{\SmileysLength}
\setlength{\SmileysLength}{\labelwidth}\addtolength{\SmileysLength}{\labelsep}
\newcommand{\hard}{\hspace*{-\SmileysLength}\makebox[0pt][r]{\IconCry}%
   \hspace*{\SmileysLength}}
\newcommand{\easy}{\hspace*{-\SmileysLength}\makebox[0pt][r]{\IconRazz}%
   \hspace*{\SmileysLength}}

\begin{document}

List of problems:
\begin{enumerate}
   \item A normal problem
   \item \easy An easy problem
   \item \hard This one is hard 
   \item This is normal
   \item This one too
   \item \easy Again an easy problem
   \item \hard We conclude with a hard one
\end{enumerate}

\end{document}
The smileys are written just on the left of the space reserved to labels (i.e. at a distance \SmileysLength=\labelwidth+\labelsep of the item text). You may adjust the length \SmileysLength at your will. Take the jpg files from my preceding post.
dmt
Posts: 82
Joined: Sat Mar 15, 2008 8:13 pm

Re: Adding icons to left of enumerate items

Post by dmt »

Thanks, that's exactly what I wanted ... but do you think you could give me a quick explanation of how it works. I can't follow all of it and to be able to modify it well later for my own needs I would like to understand it.

A brief description of the steps should be okay, if you don't mind.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Adding icons to left of enumerate items

Post by Juanjo »

I first define \IconCry and \IconRazz, which are merely two commands to insert the smileys. You could replace them, for example, by the \frownie and \smiley commands defined in the wasysym package. Then I define \easy and \hard for actually introducing the smileys in the right place.

When LaTeX finds something like

Code: Select all

\item \easy Statement of an easy problem
LaTeX first process the \item command. So it starts a new line, leaves some space and writes the corresponding label (say "3.", for example). LaTeX then process the first command in \easy or \hard, that is, \hspace*{-\SmileysLength}, which instructs LaTeX to jump back along the same line a distance equal to \SmileysLength. Next LaTeX writes a zero-width box with the smiley. So the insertion point (where LaTeX puts the next character) does not move. Finally LaTeX jumps again the same distance, arriving at exactly the same point it was before the first jump, and writes the statement of the problem.

Now, the length \SmileysLength is equal to \labelwidth+\labelsep, which are two of the several parameters which control lists. \labelwidth is the space reserved for writing the label of each item, whereas \labelsep is the separation distance between the label and the first character of the item text.

Finally, it is important to understand the use of zero-width boxes, since they allow to achieve some special effects, as in this case. The \makebox and \framebox commands have an optional argument to tell LaTeX which is the width of the resulting box. A command like \makebox[2cm]{text} constructs a 2cm wide box, independently of the length of the text which contains. If the text is short, there will be white space filling the box, but if the text is long, it will extend out of the box, superimposed to the surrounding text. Both commands have a second optional argument to control to where the contained text should be adjusted: r (right), l (left) or c (centered, default). Depending on this parameter, for short text, the filling blank space is on the right, on the left or on both sides of the box. For long text, it marks the side of the box through which the text will expand out.
dmt
Posts: 82
Joined: Sat Mar 15, 2008 8:13 pm

Re: Adding icons to left of enumerate items

Post by dmt »

Thanks, that was really helpful. Just a couple more short questions about what you did.

Why is there an asterisk after \hspace?

And why is there a % in your \hard and \easy commands?

And since you jump to the left exactly the distance of the label and labelsep and then right justify, why isn't the smiley directly up against the number? is there automatic character spacing added by latex?

Thanks again, you are really, really helping me learn about latex here.

Oh, and playing around with it I came up with one additional question.

I have a graphic icon that sits a little "high" on the line. I would like to be able to lower it's lower edge a little bit below the baseline for that line. Is there a way to do this easily?
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Adding icons to left of enumerate items

Post by Juanjo »

dmt wrote:Why is there an asterisk after \hspace?
The \hspace* space command always leaves space, even in places where \hspace does not, as the beginning of lines. Compile this to see it:

Code: Select all

Text text text. \\
\hspace{2cm}More text text text.\\
\hspace*{2cm}Even more text text text.
Anyway, in the definition of \easy and \hard, you can simply use \hspace.
dmt wrote:And why is there a % in your \hard and \easy commands?
When LaTeX finds the % symbol stops processing characters in the same line of the source file. This fact allows to intersperse the code with comments. But it is also helpful to divide a long line of code into several ones without introducing spurious blank spaces. Take into account that the spaces or the carriage return at the end of a line in the source file produces a space in the document. Check what happens if you remove % in the definition of \hard: the text of the corresponding list items will not be properly aligned, due to the extra space written after the smiley.
dmt wrote:And since you jump to the left exactly the distance of the label and labelsep and then right justify, why isn't the smiley directly up against the number? is there automatic character spacing added by latex?
The label of each item is typeset right justified in a box of length \labelwidth. If the label does not completely fill the box, you see blank space on its left. However, if you have, say, 100 problems, things look different. Compile this:

Code: Select all

\begin{enumerate}
  \setcounter{enumi}{100}
   \item A normal problem
   \item \easy An easy problem
   \item \hard This one is hard 
\end{enumerate}
dmt wrote:I have a graphic icon that sits a little "high" on the line. I would like to be able to lower it's lower edge a little bit below the baseline for that line. Is there a way to do this easily?
You can use the \raisebox command. It has two mandatory arguments: the length you want move up or down a text and the text itself. Compare Text with \raisebox{5pt}{Text} and \raisebox{-5pt}{Text}. Suppose that you want to raise down the "razz" icon by 15pt. It would suffice the following redefinition of \IconRazz:

Code: Select all

\newcommand{\IconRazz}{\raisebox{-15pt}{\includegraphics[width=1em]{icon_razz.jpg}}}
dmt
Posts: 82
Joined: Sat Mar 15, 2008 8:13 pm

Re: Adding icons to left of enumerate items

Post by dmt »

Thanks again! That cleared everything up for me.
Post Reply