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.

I'm looking for the following sort of effect in my document:

enter image description here

Or something like this:

Does anybody read me? I'm lonely. :(

Is there a package that lets me do this?

share|improve this question
    
\begin{itemize}? –  Seamus Mar 15 '11 at 22:48
1  
\lstset{showtabs=true,tab=\rightarrowfill} from package listings is close. –  Andrey Vihrov Mar 15 '11 at 23:26

1 Answer 1

up vote 9 down vote accepted

The below code makes tabs active and increments a counter every time one is encountered. At the beginning of a paragraph, a paragraph hook is added that removes the box that is inserted by an indentation, checks the depth of the indentation, sets \leftskip to the depth times \parindent, and then adds a arrow of the appropriate width to the left.

\documentclass{article}
\usepackage{everyhook}
\usepackage{lipsum}
\newcount\indentdepth
\newdimen\savedleftskip
\catcode`\^^I\active
\newenvironment{tabs}{%
    \savedleftskip=\leftskip
    \catcode`\^^I\active
    \def^^I{%
        \ifhmode
            \par
        \fi
        \advance\indentdepth by1
    }%
    \PushPreHook{par}{%
        {\setbox0\lastbox}% kill the indent
        \ifnum\indentdepth>0
            \leftskip\dimexpr\savedleftskip+\parindent*\indentdepth\relax
            \llap{\hbox to\leftskip{\rightarrowfill}}%
        \fi
    }%
    \def\par{%
        \endgraf
        \indentdepth=0
        \leftskip=\savedleftskip
    }%
    \indentdepth=0
}{%
    \par
    \PopPreHook{par}%
}
\catcode`^^I10
\begin{document}
\begin{tabs}
No indent on the text here.
        One indent.
                Two indent.
                More.
                Even More.
                Blah.
        Back to one indent.

Back to no indent.
        \lipsum[1]
                \lipsum[2]
\end{tabs}
\end{document}

Note that there is a blank line before Back to no indent. The reason for this is that unless TeX pays attention to the end of lines (e.g., by using \obeylines), there's no way to distinguish end of line from a space. Any of the indented material can be distinguished because the tab character causes the paragraph to end.

If you copy and paste this code, it's important that you change the 8 space indentation in the use of the tabs environment into actual "hard" tabs.

Here's a second way to do it that looks like your second example. Here, a newline starts a new line in the output. Any line that is taller than a \strut will not have the appropriate rule drawn in front of it.

\documentclass{article}
\usepackage{everyhook}
\newdimen\tabwidth
\newcount\indentdepth
\tabwidth=\parindent
\catcode`\^^I\active
\newenvironment{tabs}{%
    \catcode`\^^I\active
    \def^^I{\advance\indentdepth by1}%
    \def\par{\endgraf\indentdepth=0}%
    \PushPreHook{par}{%
        \ifnum\indentdepth>0
            \hbox to\dimexpr\tabwidth*\indentdepth{%
                \hfill\vrule height\ht\strutbox depth\dp\strutbox\ %
            }%
        \fi
    }%
    \parindent=0pt
    \obeylines
    \indentdepth=0
}{%
    \PopPreHook{par}%
}
\catcode`\^^I10
\begin{document}
\begin{tabs}
No indent on the text here.
        One indent.
                Two indent.
                More
                Even More.
                Blah.
        Back to one indent.
Back to no indent.
\end{tabs}
\end{document}

Again, be sure to add the tabs back in.

share|improve this answer
    
Thanks, this is excellent! –  Mana Mar 16 '11 at 12:58

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.