4

Is there a way to use the listings environment, without resorting to a specific language formatting? (but instead use the usual Latex commands being formatted correctly.)

The reason I want to do it is because I want to use a specific feature in the listings package (line numbering) for regular text.

EDIT: Here is a MWE that doesn't work for me:

 \documentclass{article}

 \usepackage{tcolorbox}
 \usepackage{lineno}

 \begin{document}

 \begin{tcolorbox}[middle=0mm,boxsep=0mm]

 \begin{linenumbers}

 \begin{itemize}
 \item x
 \item y
 \end{itemize}

 \end{linenumbers}

 \end{tcolorbox}

 \end{document}

What I want to have is a line number next to each line in the itemized list, appearing on the left margin inside the tcolorbox environment. Is there a way to do it? (right now no line numbers appear at all.)

  • 1
    Have you considered the lineno package? It provides line numbering for regular text. – Nico Boni Oct 7 '14 at 15:54
  • @dcmst the problem is that lineno does it at a global way for the whole document. I want to do it for a constrained environment, limited in scope. – kloop Oct 7 '14 at 15:57
  • 1
    the line numbering can be made local enclosing \linenumbers into a group or using the linenumbers env. – Nico Boni Oct 7 '14 at 15:59
  • @dcmst I wasn't aware of this environment, but it is not working for me. see MWE in my question. – kloop Oct 7 '14 at 16:08
4

You can do something like the following, what you need is the internallinenumbers macro:

\documentclass{article}

 \usepackage{tcolorbox}
 \usepackage{lineno}

 \begin{document}

 \begin{tcolorbox}[middle=0mm,boxsep=0mm]
  \setlength\linenumbersep{1cm} %sets the distance of the line numbers from the margin
  \begin{internallinenumbers}
   \begin{itemize}
    \item x
    \item y
   \end{itemize}
  \end{internallinenumbers}
 \end{tcolorbox}
 \end{document}

enter image description here

Or if you want the number to be printed inside the tcolorbox, just change the value of linenumbersep:

 \begin{tcolorbox}[middle=0mm,boxsep=0mm]
  \setlength\linenumbersep{-.1cm} %sets the distance of the line numbers from the margin
  \begin{internallinenumbers}
   \begin{itemize}
    \item x
    \item y
   \end{itemize}
  \end{internallinenumbers}
 \end{tcolorbox}

enter image description here

You need to be careful if you put line numbers inside as they can overlap with the box content.

Edit.

To change the style of the numbers use:

\renewcommand\linenumberfont{\normalsize} % will set the font to \normalsize, add the code you need

To add a semi-colon after the numbers use:

\renewcommand\thelinenumber{\arabic{linenumber};}
  • thanks, that could work for me. in attempt to make it more "elegant" -- is there any way to set the linenumbersep logically (relative to the frame of the tcolorbox) instead of having to set it with a specific absolute distance? also, is there a way to increase the font of the numbers slightly and add semicolons after the numbers? – kloop Oct 7 '14 at 16:51
  • @kloop the \linenumbersep already is relative to the frame of the tcolorbox. If you don't set it you'll see that it goes exactly over the frame. So 1cm means "1cm left of the frame" and -1cm means "1cm right of the frame". For the other questions see the edited answer. – Nico Boni Oct 7 '14 at 16:59

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.