2

I'm attempting to create an environment that wraps every paragraph in a different environment. For example, if I were wrapping each paragraph in flushleft it would look something like this:

\documentclass{article}

\newcounter{conversationpar}
\newenvironment{conversation}{
    \setcounter{conversationpar}{0}
    \everypar={
        \refstepcounter{conversationpar}
        \ifnum\value{conversationpar}>1\end{flushleft}\fi
        \begin{flushleft}
    }
}{
    \end{flushleft}
}

\begin{document}
\begin{conversation}
Let her hang me: hee that is well hang'de in this world, needs to feare no colours.

Make that good.

He shall see none to feare.
\end{conversation}
\end{document}

This does not work, but in theory I'd like it to result in the same as doing something like this:

\begin{flushleft}
Let her hang me: hee that is well hang'de in this world, needs to feare no colours.
\end{flushleft}

\begin{flushleft}
Make that good.
\end{flushleft}

\begin{flushleft}
He shall see none to feare.
\end{flushleft}

Thanks for your help.

3
  • Wouldn't \setlength{\parskip}{\baselineskip} also do what you want? Commented Aug 15, 2022 at 22:10
  • I'm not specifically looking for flushleft, that was just an example. Just "some environment".
    – user10744
    Commented Aug 16, 2022 at 11:39
  • I see. So, then my below answer should still work, but you should be careful. It might break in certain use cases. It depends on how the enviroment you wrap around the sections uses the contents it is provided. Commented Aug 16, 2022 at 11:42

1 Answer 1

3

I am not sure how you wish to use this eventually, but this code would split the body of the conversation environment at every \par and place every section inside a flushleft environment:

\documentclass{article}

\ExplSyntaxOn

% create a macro that will hold every paragraph of the evironment's body as a sequence
\seq_new:N \l__myconv_conversationpars_seq

% create a new environment macro, +b is a pseudo-argument that represents the body,
% the + denotes that the body may contain \pars,
% you can refer to the body using #1 (since it is the first argument statement)
\NewDocumentEnvironment{conversation} { +b } {

    % split the body (provided by #1) at every \par and 
    % store the single parts as sequence in the previously defined macro
    \seq_set_split:Nnn \l__myconv_conversationpars_seq { \par } { #1 } 

    % for each item in the sequence, place it inside a flushleft environment
    \seq_map_inline:Nn \l__myconv_conversationpars_seq { 
        \begin{flushleft} 
            ##1 
        \end{flushleft} 
    }
} { }

\ExplSyntaxOff

\begin{document}

\begin{conversation}
Let her hang me: hee that is well hang'de in this world, needs to feare no colours.

Make that good.

He shall see none to feare.
\end{conversation}

\end{document}

Edit

From the comments I learned that you wanted to provide two additional mandatory arguments to your environment and use the first or the second, depending on whether the current item is an odd or an even one. I added this to the above code:

\documentclass{article}

\ExplSyntaxOn

\seq_new:N \l__myconv_conversationpars_seq

% add two mandatory arguments, the body now being the third argument 
\NewDocumentEnvironment{conversation} { m m +b } {
    \seq_set_split:Nnn \l__myconv_conversationpars_seq { \par } { #3 } 

    % for each item in the sequence, return index as ##1 and the item as ##2
    \seq_map_indexed_inline:Nn \l__myconv_conversationpars_seq { 
        \begin{flushleft} 
            % test whether the index is odd, 
            % if yes, use value of first mandatory argument stored in #1,
            % if not, use value of first mandatory argument stored in #2
            % (~ denotes a space)
            \int_if_odd:nTF { ##1 } { #1 } { #2 } : ~ 
            ##2 
        \end{flushleft} 
    }
} { }

\ExplSyntaxOff

\begin{document}

\begin{conversation}{odd}{even}
Let her hang me: hee that is well hang'de in this world, needs to feare no colours.

Make that good.

He shall see none to feare.
\end{conversation}

\end{document}

enter image description here

13
  • I'm attempting to parse through this and figure out what's going on, I've found the documentation for NewDocumentEnvrionment but it seems like whatever this is is more complicated than normal LaTeX, so bear with me as I try to figure out what's happening and how these new sequences work. Thanks!
    – user10744
    Commented Aug 16, 2022 at 11:57
  • 1
    @SamWhited It is totally fine to learn step by step. You are right, the syntax for ``\NewDocumentEnvrionment` is a bit different that that for \newenvironment. But it is quite straight forward. The +b denotes the body containing \pars. So, in case you add more arguments, such as m m +b, the body would then be in #3 an not in #1. The ##1 on the other hand stands for every \par that is wrapped by the new environment. You don't need to change this. Commented Aug 16, 2022 at 12:04
  • +b is a magical thing that somehow means body but is just 2 random characters and none of this is logically consistent in any way, so I wouldn't call it straightforward :) I did figure out how the arguments work, but previously (I removed this from the question because I thought I knew how it works, but now this syntax is entirely different) I had a value being put in the body that would alternate between the two arguments every time. Previously I was counting whether the sequence was odd or even, but with these new sequences I don't see a good way to do that. I'm still reading though. Thanks!
    – user10744
    Commented Aug 16, 2022 at 12:07
  • @SamWhited You are right, it is maybe not that straightforward ... I added a few explanatory comments to my code and also tried to incorporate the two additional arguments to a variation of the code. I hope that this can help you! Commented Aug 16, 2022 at 12:25
  • Thank you! The comments are very helpful and I'm not sure that I ever would have made it to this solution, so I really appreciate it. Even after reading about some of this a lot of it still really didn't make any sense to me.
    – user10744
    Commented Aug 16, 2022 at 13:13

You must log in to answer this question.