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.

How can I insert a special character or a symbol in the beginning of each line in a verbatim environment? In particular, there is a constraint that applies to the verbatim environment: it must be of a special type (given below); it cannot be any of the standard verbatim environments, e.g. from packages fancyvrb or listings..

I am working on developing an environment to format email correspondence. It is based on a modification of the standard verbatim environment without the \obeylines part, see "Format a verbatim paragraph"

As the code has mainly been developed by Frank Mittelbach I am not sure what would be the minimal working example.. So I think I have to include the whole environment. Here is an example:

\documentclass{article}
\usepackage{xstring}
\makeatletter
\newdimen\outerparindent
\def\myverbatim#1{%
  \IfStrEq{#1}{c}{\renewcommand{\verbatim@font}{\slshape\ttfamily}}
  {\renewcommand{\verbatim@font}{\upshape\ttfamily}}%
  \def\do@noligs##1{%
     \catcode`##1\active
     \begingroup
       \lccode`\~`##1\relax
       \lowercase{\endgroup\def~{\leavevmode\kern\z@\char`##1 }}}%
   \outerparindent\parindent
   \def\obeylines{\rightskip=0pt plus 1fil
                  \parindent=\outerparindent
                  \let\par\@@par
                  \leavevmode\indent}%
   \def\@xobeysp{\penalty\z@\mbox{}\space\penalty\z@}%  
  \@verbatim
  \@myverbatimescape\@myverbatimnewline
  \frenchspacing\@vobeyspaces\@xmyverbatim}
\let\endmyverbatim\endverbatim
\begingroup
\catcode`\^^M=\active%
\gdef\@myverbatimnewline{\catcode`\^^M=\active \let^^M\@xmyverbatimnewline}%
\gdef\@xmyverbatimnewline{\@ifnextchar ^^M{\@myverbatimpar}{\@xobeysp}}%
\gdef\@ymyverbatimnewline{\@ifnextchar ^^M{\@myverbatimpar}{}}%
\gdef\@myverbatimpar ^^M{\par%
                         \vskip\baselineskip%
                         \@ymyverbatimnewline}%
\gdef\@zmyverbatimnewline{\@ifnextchar ^^M{\@zmyverbatimpar}{}}%
\gdef\@zmyverbatimpar^^M{\@ifnextchar ^^M{\@myverbatimpar}{}}%
\endgroup
\begingroup \catcode `|=0 \catcode `[= 1
\catcode`]=2 \catcode `\{=12 \catcode `\}=12
\catcode`\\=13
|gdef|@myverbatimescape[|catcode`|\|active|let\|@myverbatimbslash]
|gdef|@myverbatimbslash[|@ifnextchar\[|@xmyverbatimbslash][|string\]]
|gdef|@xmyverbatimbslash\[|\]
|catcode`|^^M=|active
|long|gdef|@xmyverbatim#1\end{myverbatim}[|@zmyverbatimnewline#1^^M|vskip-|lastskip|vskip|z@skip|end[myverbatim]]%
|endgroup
\makeatother
\begin{document}
\begin{myverbatim}{c}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed orci quis urna volutpat tincidunt. Praesent ultrices commodo auctor. Nunc vel nulla sed arcu aliquet faucibus quis nec ante. Nam elementum neque vitae sem fringilla a euismod eros malesuada. Duis orci eros, tincidunt et venenatis at, tristique in tortor
\end{myverbatim}
\begin{myverbatim}{n}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed orci quis urna volutpat tincidunt. Praesent ultrices commodo auctor. Nunc vel nulla sed arcu aliquet faucibus quis nec ante. Nam elementum neque vitae sem fringilla a euismod eros malesuada. Duis orci eros, tincidunt et venenatis at, tristique in tortor
\end{myverbatim}
\end{document}

The current output: enter image description here

(Goal: I would like to insert a special symbol like > or | in the beginning of each line of the first paragraph (i.e. in the beginning of each line with slanted text))

More specifically: How can I modify the environment such that if the parameter to the myverbatim environment is, say, the character c, then each line is preceded by a symbol, say >, if the parameter is not c then no symbol is inserted in the beginning of the line..

I have studied David's answer to this post "Environment prints same character at beginning of each output line" but I could not get it to work for my case.. Maybe there is a simpler solution?

(See also: How to format an email conversation?)

share|improve this question
    
You can use the package linegoal. Instead of numbering print a symbol. –  Marco Daniel May 19 '13 at 22:00
    
    
@Werner Yes that is a solution for simple cases. However, since it is using the listings package it will run into the problem with line-breaking and -joining, see Format a verbatim paragraph.. –  Håkon Hægland May 22 '13 at 8:09
    
Is there a reason why you couldn't use the fancyvrb package and its macro \FancyVerbFormatLine (explained on p.6 of the package's user guide) to insert some character at the start of each line? In the user guide, examples are given which take one argument (the line to be printed); it's straightforward to make this a two-argument macro. –  Mico May 22 '13 at 18:07
    
@Mico I do not the fancyvrb package has line breaking, see e.g. How to force line breaks within a fancyvrb verbatim environment? and Format a verbatim paragraph .. –  Håkon Hægland May 22 '13 at 18:27

2 Answers 2

up vote 3 down vote accepted

I found a possible workaround using the mdframed package:

\documentclass{article}
\usepackage{mdframed,xstring}
\makeatletter
\newdimen\outerparindent
\newcommand{\myverbatim}[1]{%
  \IfStrEq{#1}{c}{
    \begin{mdframed}[linewidth=1,topline=false,rightline=false,bottomline=false]
      \renewcommand{\verbatim@font}{\slshape\ttfamily}
      \gdef\end@myverbatim{\end{mdframed}}
}
  {\renewcommand{\verbatim@font}{\upshape\ttfamily}
    \gdef\end@myverbatim{}
}%
  \def\do@noligs##1{%
     \catcode`##1\active
     \begingroup
       \lccode`\~`##1\relax
       \lowercase{\endgroup\def~{\leavevmode\kern\z@\char`##1 }}}%
   \outerparindent\parindent
   \def\obeylines{\rightskip=0pt plus 1fil
                  \parindent=\outerparindent
                  \let\par\@@par
                  \leavevmode\indent}%
   \def\@xobeysp{\penalty\z@\mbox{}\space\penalty\z@}%  
  \@verbatim
  \@myverbatimescape\@myverbatimnewline
  \frenchspacing\@vobeyspaces\@xmyverbatim}

\let\endmyverbatim\endverbatim
\begingroup
\catcode`\^^M=\active%
\gdef\@myverbatimnewline{\catcode`\^^M=\active \let^^M\@xmyverbatimnewline}%
\gdef\@xmyverbatimnewline{\@ifnextchar ^^M{\@myverbatimpar}{\@xobeysp}}%
\gdef\@ymyverbatimnewline{\@ifnextchar ^^M{\@myverbatimpar}{}}%
\gdef\@myverbatimpar ^^M{\par%
                         \vskip\baselineskip%
                         \@ymyverbatimnewline}%
\gdef\@zmyverbatimnewline{\@ifnextchar ^^M{\@zmyverbatimpar}{}}%
\gdef\@zmyverbatimpar^^M{\@ifnextchar ^^M{\@myverbatimpar}{}}%
\endgroup
\begingroup \catcode `|=0 \catcode `[= 1
\catcode`]=2 \catcode `\{=12 \catcode `\}=12
\catcode`\\=13
|gdef|@myverbatimescape[|catcode`|\|active|let\|@myverbatimbslash]
|gdef|@myverbatimbslash[|@ifnextchar\[|@xmyverbatimbslash][|string\]]
|gdef|@xmyverbatimbslash\[|\]
|catcode`|^^M=|active
|long|gdef|@xmyverbatim#1\end{myverbatim}[|@zmyverbatimnewline#1^^M|vskip-|lastskip|vskip|z@skip|end@myverbatim|end[myverbatim]]%
|endgroup
\makeatother

\begin{document}
\begin{myverbatim}{c}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed orci quis urna volutpat tincidunt. Praesent ultrices commodo auctor. Nunc vel nulla sed arcu aliquet faucibus quis nec ante. Nam elementum neque vitae sem fringilla a euismod eros malesuada. Duis orci eros, tincidunt et venenatis at, tristique in tortor
\end{myverbatim}
\begin{myverbatim}{n}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed orci quis urna volutpat tincidunt. Praesent ultrices commodo auctor. Nunc vel nulla sed arcu aliquet faucibus quis nec ante. Nam elementum neque vitae sem fringilla a euismod eros malesuada. Duis orci eros, tincidunt et venenatis at, tristique in tortor
\end{myverbatim}

\end{document}

and the output:

enter image description here

I think it looks not so bad.. In addition the mdframed package allows the environment to cross page breaks :)

share|improve this answer

Are you tied to the verbatim environment explicitly, or only the functionality of the verbatim environment? If the latter, this example below formats text in a verbatimlike way, obeying wrapping and margins. With the [c] option, it puts a vertical bar to the left of the text, which, while not a character in the first column of every line, would seem to satisfy your underlying need to somehow identify prior correspondence. It also takes an optional argument, which can further tailor the appearance of the result. [Thanks to Joseph Wright at Preserving (and Controlling) Expandedness for the \detokenize pointer]

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{color}
\usepackage{tabularx}
\newcommand\display[3][]{%
\noindent\tabcolsep=3pt\renewcommand\arraystretch{0}\arrayrulewidth=.5ex%
\if c#2%
  \begin{tabularx}{\textwidth}{|X}%
  \raggedright\color{blue}\ttfamily\slshape#1\detokenize\expandafter{#3}%
  \end{tabularx}%
\else%
  \tabcolsep=0pt%
  \begin{tabularx}{\textwidth}{X}%
  \raggedright\ttfamily#1\detokenize\expandafter{#3}%
  \end{tabularx}%
\fi
}
\begin{document}
\noindent Textwidth bar
\raggedright
\noindent\rule{\textwidth}{0.5ex}
\vspace{1em}

\display{n}{%
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed orci
quis urna volutpat tincidunt. Praesent ultrices commodo auctor. Nunc vel
nulla sed arcu aliquet faucibus quis nec ante. Nam elementum neque vitae
sem fringilla a euismod eros malesuada. Duis orci eros, tincidunt et
venenatis at, tristique in tortor and other \verbatim characters^ $ &
\\ etc.%
}

\display[\itshape\color{red}]{n}{%
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed orci
quis urna volutpat tincidunt. Praesent ultrices commodo auctor. Nunc vel
nulla sed arcu aliquet faucibus quis nec ante. Nam elementum neque vitae
sem fringilla a euismod eros malesuada. Duis orci eros, tincidunt et
venenatis at, tristique in tortor and other \verbatim characters^ $ &
\\ etc.%
}

\display{c}{%
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed orci
quis urna volutpat tincidunt. Praesent ultrices commodo auctor. Nunc vel
nulla sed arcu aliquet faucibus quis nec ante. Nam elementum neque vitae
sem fringilla a euismod eros malesuada. Duis orci eros, tincidunt et
venenatis at, tristique in tortor and other \verbatim characters^ $ &
\\ etc.%
}

\end{document}

enter image description here

share|improve this answer
    
Thank you for the effort! I realized now that I found the other solution using mdframed at the same time as you posted your solution.. However, as far as I can see, it seems that the mdframed solution is better.. The mdframed solution also includes possibilities for explicit line breaks using \\, in addition empty lines in the input will produce empty lines in the output, and it will work across page breaks.. The \detokenize macro seems interesting though :) –  Håkon Hægland May 23 '13 at 17:14

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.