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 have following problem. In the code below the second parameter of new command 'chunk' is supposed to output text 'as-is' using alltt package (I need alltt because I will output some other new commands). What instead happens is that all text is displayed in one line (my guess is => parameter causes this).

\documentclass[a4paper,12pt,openany]{book}
\usepackage[croatian]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{blindtext}
\usepackage[a4paper, inner=1.5cm, outer=3cm, top=2cm, bottom=3cm, bindingoffset=1cm]{geometry}
\usepackage{amsmath}
\usepackage{microtype}
\usepackage{alltt}

\newcommand{\chunk}[2]{ 
    \label{#1}
    $\langle\textit{#1}\ \rangle\equiv$
    \begin{alltt}
        #2
    \end{alltt}
}

\begin{document}

\chunk{Example}{
    some ex ample
    \{
        som e t e xt
            te xt
    \}
}

\end{document}

Now, the quick fix I came up with would remove usage of alltt from the chunk command and instead use alltt in the document directly like this

\chunk{Example}
\begin{alltt}
    some ex ample
    \{
        som e t e xt
            te xt
    \}
\end{alltt}

but this approach is ugly and leads to code repetition so I would like to avoid it. Any help is appreciated.

share|improve this question
add comment

1 Answer

up vote 8 down vote accepted

The LaTeX style for a command taking large chunks of text, especially if (as here) you need to change the scanning conventions is to use an environment not a command. If you did

\newenvironment{chunk}[1]
{%
    \label{#1}%
    $\langle\textit{#1}\ \rangle\equiv$%
    \begin{alltt}}
{\end{alltt}}

then line endings in the environment would be preserved. Also note that you need % at the end of lines in macro definitions or you will get spurious spaces in the output.

\begin{chunk}{Example}
    some ex ample
    \{
        som e t e xt
            te xt
    \}
\end{chunk}
share|improve this answer
    
Thank you, just what I needed (will upvote when I earn 15 rep). Looks like I have some reading about environments to do. –  Marin Jul 12 '12 at 20:33
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.