1

LaTEX newbie here... I am trying to quote portions of chat logs and get them to look like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed iaculis orci augue, id finibus nunc feugiat egestas. Ut at consectetur orci.

    (...)
    Bob: This is the first line of my message
        and this the second one
        and this the third
    Alice: Hi Bob
    (...)

Morbi laoreet gravida elit vitae posuere. Mauris vel urna nec elit cursus lacinia.

And this is the code that I am using now:

 %!TEX program = xelatex
\documentclass[12pt,a4paper,oneside]{report}

\newenvironment{chat} {
    \list{}{
        \topsep 2em
        \parsep -1em
        \rightmargin \leftmargin
        \advance\leftmargin 1.5em
        \newcommand\who[1]{\item[{##1}:]}
    }
    \item[]
}{\endlist}

\begin{document}

\chapter{Chapter}

\section{title}

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed iaculis orci augue, id finibus nunc feugiat egestas.
Ut at consectetur orci.

\begin{chat}
(...)\\
\who{Bob} This is the first line of my message\\
and this the second one\\
and this the third\\
\who{Alice} Hi Bob\\
(...)
\end{chat}

Morbi laoreet gravida elit vitae posuere.
Mauris vel urna nec elit cursus lacinia.

\end{document}

Is this the right approach? And also, I tried to add the (...) before and after directly inside the \newenvironment, so that I don't have to include them any time, but I couldn't get them to align properly. Any help?

Thanks

3
  • 2
    it would be easier to test your code if you posted a test document not a fragment. you do not want the \item[] in the environment definition but apart from that it looks Ok if it gives the layout you want, also I wouldn't use \\ but rather use \who{bob} each time. Commented Apr 26, 2018 at 15:09
  • Thank you, @DavidCarlisle. I added the full code now. Do you mean I should move elsewhere \newcommand\who[1]{\item[{##1}:]}? And also the issue with using \who{bob} each time means that the name would repeat, while I want to group messages that are sent separately by the same person together using the indentation. Also in my code the (...) are not aligned with the rest of the items - any idea how to change this?
    – cellulosa
    Commented Apr 26, 2018 at 15:30
  • no I mean the \item[] that you have in the enviornmemt definition. I would use \who{bob} every time and if you want special formatting for repeat comments that can be done (liek bibliography formats that use a dash for second publication by same author rather than repeat the name Commented Apr 26, 2018 at 16:37

2 Answers 2

2

This will use your \who command, but doesn't work with lists (uses hangindent and relies on a parindent > 0pt).

chatlog

 %!TEX program = xelatex
\documentclass[12pt,a4paper,oneside]{report}

\newenvironment{chat} {
    \newcommand\ellipsis{\par\hangindent=0pt\hangafter=0 (\dots)\par}
    \newcommand\who[1]{\par\hangindent=2.5em\hangafter=1 ##1:}
    \par\vskip2em
}{%
    \par
    \hangindent=0pt\hangafter=0
    \vskip2em
}

\begin{document}

\chapter{Chapter}

\section{title}

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed iaculis orci augue, id finibus nunc feugiat egestas.
Ut at consectetur orci.

\begin{chat}
\ellipsis
\who{Bob} This is the first line of my message\\
and this the second one\\
and this the third
\who{Alice} Hi Bob
\ellipsis
\end{chat}

Morbi laoreet gravida elit vitae posuere.
Mauris vel urna nec elit cursus lacinia.

\end{document}
2
  • Great approach! I've tweaked it a little so that I can do without the `\\` (posted below)
    – cellulosa
    Commented Apr 27, 2018 at 9:40
  • @cellulosa Thanks. I did not include obeylines, because it has some drawbacks for longer lines (i.e. concerning line breaking).
    – TeXnician
    Commented Apr 27, 2018 at 10:07
0

Following @TeXnician approach, I've ended up using this code, that uses \ obeylines so that I do not need to add the \\ on the relevant lines:

%!TEX program = xelatex
\documentclass[12pt,a4paper,oneside]{report}

\newenvironment{chat} {
    \newcommand\ellipsis{
        \parindent=1.5em
        \hangindent=0pt
        \hangafter=0
        (\dots)
    }
    \newcommand\who[1]{
        \parindent=1.5em
        \hangindent=2.5em
        \hangafter=1
        ##1:
        \parindent=2.5em
        \hskip1pt
    }
    \par\vskip2em
    \obeylines
}{
    \hangindent=0pt\hangafter=0
    \vskip2em
}

\begin{document}

\chapter{Chapter}

\section{title}

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed iaculis orci augue, id finibus nunc feugiat egestas.
Ut at consectetur orci.

\begin{chat}
\ellipsis
\who{Bob} This is the first line of my message
and this the second one
and this the third
\who{Alice} Hi Bob
\ellipsis
\end{chat}

Morbi laoreet gravida elit vitae posuere.
Mauris vel urna nec elit cursus lacinia.

\end{document}

Result:

Build from code above

You must log in to answer this question.

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