1

I want to add something to each newline, like this:

\documentclass{article}
\newenvironment{mystuff}{dark magic goes here}{or here}
\begin{document}
\begin{mystuff}
first row
second row
\end{mystuff}
\end{document}

and I want Latex to render it like this:

first row -and this was added by mystuff environment
second row -and this was added by mystuff environment

How do I do it?

1
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – jub0bs
    Commented Mar 17, 2014 at 10:38

2 Answers 2

1

As Henri Menke says, the ^^M trick is risky, but it does work in a pinch. I edited this to use \obeylines (essentially equivalent and risky) which simplifies the code a little. I also removed the \makeatletter...\makeatother commands. I altered the code slightly so that you can specify, on the fly, the text that is added:

\documentclass{article}
\pagestyle{empty}% for cropping
\usepackage{blindtext}

%\def\specialccr{\mytext\newline}
\def\specialccr{\mytext\par}

{\obeylines
    \gdef\specialcr{\obeylines \let^^M=\specialccr}%
}

\newenvironment{mystuff}[1]{%
    \def\mytext{#1}%
    \parindent0pt
    \leftskip3em
    \everypar={$\bullet$\ }
    \par
    \noindent
    \specialcr}{%
}

\begin{document}
\blindtext[1]

\begin{mystuff}{\ -- and this was added by mystuff environment}% <- % Necessar
first row
second row
third row
\end{mystuff}

\blindtext[1]
\end{document}

One final edit. The original used \newline which does not start new paragraphs. I substituted \par which in turn allows all sorts of interesting formatting:

enter image description here

1

I am using an active end of line character ^^M. This is not a good solution, but it works.

\documentclass{article}
\pagestyle{empty}% for cropping
\usepackage{blindtext}
\makeatletter
\def\special@cr{!\newline}
{\catcode`\^^M=\active%
    \gdef\specialcr{\catcode`\^^M=\active \let ^^M=\special@cr}%
}
\makeatother
\newenvironment{mystuff}{\par\noindent\specialcr}{}
\begin{document}
\blindtext[1]
\begin{mystuff}% You will want to suppress the ^^M here.
first row
second row
\end{mystuff}
\blindtext[1]
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .