帮助自定义环境引用聊天记录

帮助自定义环境引用聊天记录

我是 LaTEX 新手...我试图引用聊天记录的部分内容并让它们看起来像这样:

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.

这是我现在使用的代码:

 %!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}

这是正确的方法吗?此外,我尝试在 之前和之后直接添加 (...) \newenvironment,这样我就不必随时添加它们,但我无法让它们正确对齐。有什么帮助吗?

谢谢

答案1

这将使用您的\who命令,但不适用于列表(使用 hangindent 并依赖于 parindent > 0pt)。

聊天记录

 %!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

按照@TeXnician 方法,我最终使用了此代码,\ obeylines这样我就不需要\\在相关行中添加:

%!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}

结果:

根据上面的代码构建

相关内容