4

I have an itemize environment that I use in a figure to describe, say, an algorithm or a process. I want to include line numbers, all aligned on the left most side of the figure so that I can refer to line x and line y in the figure (in the text itself). I thought it might be possible to do it with the lineno package, but it seems like it adds lines for the whole document.

Is there a way to localize that and say have something like:

  \begin{locallineno}
   some text with line numbers
  \end{locallineno}

Also, I would rather empty lines (blank lines) not be numbered, but that seems to be well taken care of in lineno.

EDIT: For example, I could have the following environment:

  \begin{locallineno}
       \begin{itemize}
         \item First clean the dishes.
         \item Now fill the tea pot with water.
              \begin{itemize}
                  \item First open the tea pot.
                  \item Turn on the tap
               \end{itemize}
        \end{itemize}
  \end{locallineno}

I would want it to have line numbers on the left for each bullet.

  • 1
    I'd recommend the listings package. – user10274 Oct 7 '14 at 9:49
  • If the contents contain code that needs to be put in verbatim listings is among your best options. If you just want prose, you still need to specify newlines somehow, so the easiest option would just be to put \item at the beginning of each line and customising the spacing using for example the enumitem package. – Bordaigorl Oct 7 '14 at 10:07
  • @MarcvanDongen it seems like listings is for formatting source code. I am not using source code, but arbitrary itemized text... – kloop Oct 7 '14 at 10:41
  • @Bordaigorl so, I think what I would like to do is: create a new counter. reset it before every figure. change the itemize command so that it prints a number on the margin of the box it is in from the counter. Also, please note that I might have nested itemized items here, and they all should follow the same counter. I will have to think on how to do that in latex, I am not well-versed with the macros. – kloop Oct 7 '14 at 10:43
  • What about the linesnumbered option of algorithm2e? – Bordaigorl Oct 7 '14 at 11:27
4

With the lineno package you can use the linenumbers environment to locally enable numbering:

Line numbers on nested <code>itemize</code> environments

You can also use \nolinenumbers to disable numbering for the nested itemize if desired:

Line numbers for top level items only


Code: Use linenumbers environment

\documentclass[10pt,a4paper]{article}
\usepackage{lipsum}
\usepackage{lineno}

\begin{document}
\lipsum[1]
\begin{linenumbers}
       \begin{itemize}
         \item First clean the dishes.
         \item Now fill the tea pot with water.
              \begin{itemize}
                  \item First open the tea pot.
                  \item Turn on the tap
               \end{itemize}
        \end{itemize}
\end{linenumbers}
 \lipsum[1] 
\end{document}

Code: Use \nolinenumbers to disable numbering

\documentclass[10pt,a4paper]{article}
\usepackage{lipsum}
\usepackage{lineno}

\begin{document}
\lipsum[1]
\begin{linenumbers}
       \begin{itemize}
         \item First clean the dishes.
         \item Now fill the tea pot with water.
              \begin{itemize}\nolinenumbers
                  \item First open the tea pot.
                  \item Turn on the tap
               \end{itemize}
        \end{itemize}
\end{linenumbers}
 \lipsum[1] 
\end{document}
3

It can be done with the enumitem package and its wide option. I create an itemize-like environment, numitemise, with a label width that increases with the level, that is aligned on the text left margin (wide option):

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{enumitem}
\newcounter{numln}
\setlength\fboxsep{0pt}
\newlist{numitemise}{itemize}{2}
\setlist[numitemise]{wide}%
\setlist[numitemise, 1]{labelindent=0pt,labelwidth=2em, label=\stepcounter{numln}\makebox[2em]{\thenumln.\hfill\textbullet}, leftmargin=\dimexpr\labelwidth+\labelsep\relax}%
 \setlist[numitemise, 2]{labelindent=\dimexpr -2em-\labelsep\relax, labelwidth=\dimexpr 2em+\labelsep\relax, label=\stepcounter{numln}\makebox[\dimexpr\labelwidth + \labelsep\relax]{\thenumln.\hfill\textbullet}, leftmargin=\dimexpr\leftmargin+2\labelsep\relax}%

 \begin{document}

Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text.

\begin{numitemise}%{A}
  \item The text of a first short item.
  \item The text of a second, longer item. The text of a second, longer item. The text of a second, longer item.
  \begin{numitemise}%{B}
    \item And now a nested subitem. It can be short or long. It can be short or long.
    \item Another nested subitem.
  \end{numitemise}
\item Back to a third item.
\end{numitemise}

\end{document} 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.