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.

Define an enhance verse environment, where stroke Carriage return key once make a \\, twice make a \par, three times make a \medskip, four times and more make a \bigskip.

An example would be:

\begin{enhanceverse}
The furthest distance in the world
Is not between life and death
But when I stand in front of you
Yet you don't know that I love you


The furthest distance in the world
Is not when I stand in front of you
Yet you can't see my love
But when undoubtedly knowing the love from both

Yet cannot be together




---Anonymous
\end{enhanceverse}

which should output a similar appearance.

share|improve this question
1  
Welcome to TeX.sx! What is the background of such a question? (Is it a homework?) –  Andrey Vihrov Oct 21 '11 at 15:26
1  
It sounds like the verbatim environment might also be of interest –  cmhughes Oct 21 '11 at 18:35
add comment

2 Answers

up vote 11 down vote accepted

You need to make the carriage return active and use a counter as follows. Watch out for the % used in the definition. They are to mask carriage returns in the definition.

\documentclass{article}

\makeatletter

\newenvironment{enhanceverse}{%
    \verse
    \futurelet\next\@enhanceverse
}{%
    \endverse
}

\newcounter{enhanceversecr}

\begingroup
\catcode`\^^M=\active%
\gdef\@enhanceverse{%
    \catcode`\^^M=\active%
    \let^^M\@enhanceverse@cr%
}%
\gdef\@enhanceverse@cr{%
    \stepcounter{enhanceversecr}%
    \@ifnextchar^^M{}{%
        \ifcase\c@enhanceversecr%
        \or\\%
        \or\par%
        \or\par\medskip%
        \else\par\bigskip%
        \fi%
        \setcounter{enhanceversecr}{0}%
    }%
}%
\endgroup%

\makeatother

\begin{document}

\begin{enhanceverse}
The furthest distance in the world
Is not between life and death
But when I stand in front of you
Yet you don't know that I love you


The furthest distance in the world
Is not when I stand in front of you
Yet you can't see my love
But when undoubtedly knowing the love from both

Yet cannot be together




---Anonymous
\end{enhanceverse}


\end{document}

Result

share|improve this answer
    
Missing % in the \ifcase line? It may be simpler to set \endlinechar=-1. –  Bruno Le Floch Oct 21 '11 at 19:40
    
@BrunoLeFloch: Indeed (fixed now). I wonder why I didn't got an error. I wasn't sure about \endlinechar while ^^M is active, so I played it save. –  Martin Scharrer Oct 21 '11 at 19:45
2  
There's a neater way to avoid worrying about end-of-lines: say \begingroup\lccode~=\^^M \lowercase{\endgroup and do the same definitions (\gdef is not necessary, so use \def) changing ^^M into ~. Remember a closing } in place of \endgroup. Very neat the \expandafter to tokenize the following possible end-of-line, but maybe \futurelet\next can be better, as it avoids possible issues with expansion. –  egreg Oct 21 '11 at 19:59
    
@egreg: Yes, the \lowercase trick. It didn't popped into my mind. Funny, it seldom does. About the \futurelet: Maybe a dummy \@ifnextchar would be better. –  Martin Scharrer Oct 21 '11 at 20:01
1  
Well, \@ifnextchar does almost the same as \futurelet\next (but it swallows spaces). –  egreg Oct 21 '11 at 20:22
show 1 more comment

ConTeXt provides a lines environment in which the user line breaks are honored (similar to <pre> tags in HTML). FWIW, you can achieve the desired behavior by setting

\setuplines[inbetween={\null\blank}]

and then using

\startlines
 ....
\stoplines

This is not exactly same as what you want, if you have three \r, you get the same space as two empty lines, and so on. But this will also work if you have spaces on empty lines and so on.

share|improve this answer
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.