Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In the solution to Making more easy the itemized of item with tabulation system, I am counting up the number of spaces in order to determine the type of leading character to insert. However, if the I replace the leading spaces with a tab character, the solution does not work.

If I could detect the tab character in the literate, then I could have \ProcessSpace increment the counter NumOfContigousSpaces appropriately, but I don't know how to test for it?

I thought adding tabsize=4, keepspaces=true would do the job but this is not quite enough. So I attempted to use lstag@tabulator from How to automatically skip leading white spaces in listings, but was not able to get that to work.

The code below has a 4 leading spaces before the W in the first line and a tab as the leading character before the W in the second line. This produces no bullet for the line with a tab:

enter image description here

The correct output can be seen by using 4 spaces before the W in both lines:

enter image description here

Note:

  • It appears the posting a code snippet here replaces a tab with 4 spaces. So to use the MWE below you will need to replace the four leading spaces before the Wxxx with a tab character.

Code:

\documentclass{article}
\usepackage{pgf}
\usepackage{xstring}
\usepackage{listings}

\newcounter{NumOfContigousSpaces}%
\setcounter{NumOfContigousSpaces}{0}%

\newcommand{\Width}{1}%
\newcommand*{\AddApproriateBulletIfFirstChar}[1]{%
    \pgfmathtruncatemacro{\BulletType}{\arabic{NumOfContigousSpaces}/4}%
    \IfEqCase{\BulletType}{%
        {0}{\gdef\Width{1}}
        {1}{\gdef\Width{3}$\bullet$ }
        {2}{\gdef\Width{3}$\circ$ }
        {3}{\gdef\Width{3}$\times$ }
        {4}{\gdef\Width{3}$\star$ }
        {5}{\gdef\Width{3}$-$ }
    }[\gdef\Width{3}$\bullet$ ]%
    #1%
    \setcounter{NumOfContigousSpaces}{0}%
}%
\newcommand*{\ProcessSpace}{%
    \addtocounter{NumOfContigousSpaces}{1}%
    \space%
}%
\newcommand*{\ProcessTab}{%
    \addtocounter{NumOfContigousSpaces}{4}%
    \space\space\space\space%
}%


\makeatletter
\lstdefinestyle{MyItemize}{%
    basicstyle=\ttfamily,
    columns=flexible,
    tabsize=4, keepspaces=true,
     literate=%
        {\ }{{{\ProcessSpace}}}1% Count contigous spaces
        {lstag@tabulator}{{{\ProcessTab}}}4% ??? how detect a tab?
        %
        %--- much code removed here (See http://tex.stackexchange.com/questions/57939/making-more-easy-the-itemized-of-item-with-tabulation-system for full code)
        {W}{{{\AddApproriateBulletIfFirstChar{W}}}}\Width
        {x}{{{\AddApproriateBulletIfFirstChar{x}}}}\Width
}%
\makeatother

\begin{document}
\begin{lstlisting}[style=MyItemize]
    Wxxx xxx
    Wxxx xx xx
\end{lstlisting}
\end{document}
share|improve this question
    
Using \^^I is the 'safe' way to type in a tab, and will let listings do the conversion. Unfortunately, there seems to be something up with the width of characters used when columns = flexible is set, as the space used for the 'tab' bullet is different from that for the 'four space' bullet. –  Joseph Wright Jun 1 '12 at 6:32
    
@JosephWright: Please make that the answer as I was able to get it to work just fine with that. Required replacing the call to \ProcessSpace{4} with a call to \ProcessTab which is defined as \newcommand*{\ProcessTab}{\addtocounter{NumOfContigousSpaces}{4}\space\space\sp‌​ace\space} –  Peter Grill Jun 1 '12 at 19:34
add comment

2 Answers

up vote 4 down vote accepted

Using \^^I is the 'safe' way to type in a tab, and will let listings do the conversion.

Replacing the line using lstag@tabulator with the following:

    {\^^I}{{{\ProcessTab}}}4%

produces:

enter image description here

(There seems to be something up with the width of characters used when columns = flexible is set, as the space used for the 'tab' bullet is different from that for the 'four space' bullet.)

share|improve this answer
add comment

you can try something like:

\lstset{
  ...
  tab=$\bullet$\space,
    
  showtabs=true,
    
  ...
}
share|improve this answer
    
I don't want listings to replace the tab with a \bullet (as that results in two \bullets for the case where there are two leading tabs. I want the \literate to be able to detect it and treat it as 4 spaces. –  Peter Grill Jun 1 '12 at 19:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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