2

This question follows the question 282721 (\command without {} ending at the end of the line?). My new environment does three things:

  • Creates a paragraph of its kind \quote
  • Hangout the first characters of paragraph
  • Starts the rest of the paragraph at the margin.

The code of my environnment seems a little heavy (cargo cult!). Is it possible to get any easier?

My MWE :

 \documentclass[]{memoir}%The class i need to use is derived from memoir

    \def\changemargin#1#2{\list{}{\rightmargin#2\leftmargin#1\setlength{\parindent}{-1.0cm}}\item[]}
    \let\endchangemargin=\endlist
    % question 588, Vivi answer : https://tex.stackexchange.com/questions/588/how-can-i-change-the-margins-for-only-part-of-the-text/600#600

    \usepackage{tabto}%« "Tab" to a mea­sured po­si­tion in the line »

    \newenvironment{dialogue}{% 
        \begin{changemargin}{2.0cm}{2.0cm}%environment like \quote
            \par\setlength{\parindent}{-1.0cm}%for hangout par          
            \leavevmode%if not, the first line is not hang
            \TabPositions{0.0cm}%to begin the text at margin
            \newcommand\one{\par\normalfont M : \tab}%
            \newcommand\two{\par\normalfont  P : \tab\normalfont\itshape}%
            \newcommand\NormalText{\par\normalfont\tab}%
        }{\par\end{changemargin}}

    \usepackage{lipsum}

    \begin{document}
    \lipsum[4]%
    \begin{dialogue}%
        \two \lipsum[4]
        \two \lipsum[4]
        \one \lipsum[4]
        \one \lipsum[4]
        \NormalText \lipsum[4]
        \two \lipsum[4]
    \end{dialogue}
    \lipsum[4]%
    \end{document}
Amorok
  • 137

2 Answers2

3

Here is a solution

\documentclass[]{memoir}

\newenvironment{dialogue}{%
   \newcommand\one{\normalfont\item[M:]}%
   \newcommand\two{\normalfont\item[P:]\itshape}% 
   \newcommand\NormalText{\item[]\normalfont}%
   \list{}{\rightmargin2cm\leftmargin2cm\labelsep1cm}
}{\endlist}

\usepackage{lipsum}

\begin{document}
\lipsum[4]%
\begin{dialogue}%
    \two \lipsum[4]
    \two \lipsum[4]
    \one \lipsum[4]
    \one \lipsum[4]
    \NormalText \lipsum[4]
    \two \lipsum[4]
\end{dialogue}
\lipsum[4]%

\end{document}
touhami
  • 19,520
  • I chose your answer because it is simpler ... and I managed to understand it ! Thank you to you and @egreg to force me to learn more about the command \list and \ endlist! – Amorok Jan 30 '16 at 20:27
  • Oups! I just realized that the text of the first line exceeds the right margin! My attention was so focused on the positioning of the label that I had not noticed! Thank you as well. – Amorok Jan 31 '16 at 21:26
  • @Amorok sorry, it seems that this is another question. But you can try with \lipsum[3] or \lipsum[2] – touhami Jan 31 '16 at 21:48
2

Does this qualify as simpler? ;-)

\documentclass[]{memoir}
\usepackage{lipsum}

\providecommand{\one}{}
\providecommand{\two}{}
\providecommand{\NormalText}{}
\newenvironment{dialogue}
 {%
  \renewcommand{\one}{\par\makebox[0pt][r]{\makebox[1cm][l]{\normalfont M :}}\normalfont}%
  \renewcommand{\two}{\par\makebox[0pt][r]{\makebox[1cm][l]{\normalfont P :}}\itshape}%
  \renewcommand{\NormalText}{\par\normalfont}
  \list{}{\leftmargin=2cm \rightmargin=\leftmargin}\item\relax
 }
 {\endlist}

\begin{document}

\lipsum[2]
\begin{dialogue}
\two \lipsum[2]
\two \lipsum[2]

\one \lipsum[2]
\one \lipsum[2]

\NormalText \lipsum[2]

\two \lipsum[2]
\end{dialogue}

\lipsum[2]

\end{document}

Blank lines before paragraphs prefixed by \one, \two or \NormalText are optional.

enter image description here

egreg
  • 1,121,712
  • I did not see your answer :-) – touhami Jan 30 '16 at 16:49
  • @touhami Yours is good as well, possibly resulting in different vertical spacing (both can be fixed at the OP's discretion). – egreg Jan 30 '16 at 16:50
  • yes of course :-) – touhami Jan 30 '16 at 16:51
  • Thanks, like always you're fast! I try to understand and i have to ask, what is the advantage of using \providecommand? I presume it is to detect if the command is already define (error detection)? http://tex.stackexchange.com/questions/36175/what-do-newcommand-renewcommand-and-providecommand-do-and-how-do-they-differ/36177#36177 – Amorok Jan 30 '16 at 18:48
  • @Amorok No real advantage except for using \renewcommand in the environment's definition in a consistent way. – egreg Jan 30 '16 at 18:49