双列文档内的聊天气泡

双列文档内的聊天气泡

我正在尝试制作一个包含聊天记录的文档,发现了一个示例代码在这里除了 documentclass 中的 twocolumn 选项之外。

但是聊天气泡不会保留在栏内。有人知道如何纠正吗?

我想要两列页面的聊天记录。

\documentclass[11pt,a4paper, twocolumn]{article}
\usepackage[many]{tcolorbox}
\usepackage{xcolor}
\usepackage{varwidth}
\usepackage{environ}
\usepackage{xparse}

\newlength{\bubblesep}
\newlength{\bubblewidth}
\setlength{\bubblesep}{2pt}
\AtBeginDocument{\setlength{\bubblewidth}{.75\textwidth}}
\definecolor{bubblegreen}{RGB}{103,184,104}
\definecolor{bubblegray}{RGB}{241,240,240}

\newcommand{\bubble}[4]{%
  \tcbox[
    on line,
    arc=4.5mm,
    colback=#1,
    colframe=#1,
    #2,
  ]{\color{#3}#4}%
}

\ExplSyntaxOn
\seq_new:N \l__ooker_bubbles_seq
\tl_new:N \l__ooker_bubbles_first_tl
\tl_new:N \l__ooker_bubbles_last_tl

\NewEnviron{rightbubbles}
 {
  \begin{flushright}
  \sffamily
  \seq_set_split:NnV \l__ooker_bubbles_seq { \par } \BODY
  \int_compare:nTF { \seq_count:N \l__ooker_bubbles_seq < 2 }
   {
    \bubble{bubblegreen}{rounded~corners}{white}{\BODY}\par
   }
   {
    \seq_pop_left:NN \l__ooker_bubbles_seq \l__ooker_bubbles_first_tl
    \seq_pop_right:NN \l__ooker_bubbles_seq \l__ooker_bubbles_last_tl
    \bubble{bubblegreen}{sharp~corners=southeast}{white}{\l__ooker_bubbles_first_tl}
    \par\nointerlineskip
    \addvspace{\bubblesep}
    \seq_map_inline:Nn \l__ooker_bubbles_seq
     {
      \bubble{bubblegreen}{sharp~corners=east}{white}{##1}
      \par\nointerlineskip
      \addvspace{\bubblesep}
     }
    \bubble{bubblegreen}{sharp~corners=northeast}{white}{\l__ooker_bubbles_last_tl}
    \par
   }
   \end{flushright}
 }
\NewEnviron{leftbubbles}
 {
  \begin{flushleft}
  \sffamily
  \seq_set_split:NnV \l__ooker_bubbles_seq { \par } \BODY
  \int_compare:nTF { \seq_count:N \l__ooker_bubbles_seq < 2 }
   {
    \bubble{bubblegray}{rounded~corners}{black}{\BODY}\par
   }
   {
    \seq_pop_left:NN \l__ooker_bubbles_seq \l__ooker_bubbles_first_tl
    \seq_pop_right:NN \l__ooker_bubbles_seq \l__ooker_bubbles_last_tl
    \bubble{bubblegray}{sharp~corners=southwest}{black}{\l__ooker_bubbles_first_tl}
    \par\nointerlineskip
    \addvspace{\bubblesep}
    \seq_map_inline:Nn \l__ooker_bubbles_seq
     {
      \bubble{bubblegray}{sharp~corners=west}{black}{##1}
      \par\nointerlineskip
      \addvspace{\bubblesep}
     }
    \bubble{bubblegray}{sharp~corners=northwest}{black}{\l__ooker_bubbles_last_tl}\par
   }
  \end{flushleft}
 }
\ExplSyntaxOff

\title{Title of my document}
\date{2013-09-01}
\author{John Doe}

\begin{document}

\maketitle
\pagenumbering{gobble}
\newpage
\pagenumbering{arabic}

\begin{leftbubbles}
    Left-aligned gray bubbles (241, 240, 240) with black text

    Right-aligned green bubbles (103, 184,104) with white text

    Bubbles only break after a paragraph (equivalent to an enter press when chatting). Long message with multiple lines will be kept in one bubble.

    Left and right edges are round.
\end{leftbubbles}

\begin{rightbubbles}
    Single
 \end{rightbubbles}

 \begin{leftbubbles}
    Left-aligned gray bubbles (241, 240, 240) with black text

    Right-aligned green bubbles (103, 184,104) with white text

    Bubbles only break after a paragraph (equivalent to an enter press when chatting). Long message with multiple lines will be kept in one bubble.

    Left and right edges are round.
\end{leftbubbles}

\begin{rightbubbles}
    Single
 \end{rightbubbles}

 \begin{leftbubbles}
    Left-aligned gray bubbles (241, 240, 240) with black text

    Right-aligned green bubbles (103, 184,104) with white text

    Bubbles only break after a paragraph (equivalent to an enter press when chatting). Long message with multiple lines will be kept in one bubble.

    Left and right edges are round.
\end{leftbubbles}

\begin{rightbubbles}
    Single
 \end{rightbubbles}

 \begin{leftbubbles}
    Left-aligned gray bubbles (241, 240, 240) with black text

    Right-aligned green bubbles (103, 184,104) with white text

    Bubbles only break after a paragraph (equivalent to an enter press when chatting). Long message with multiple lines will be kept in one bubble.

    Left and right edges are round.
\end{leftbubbles}

\begin{rightbubbles}
    Single
 \end{rightbubbles}

 \begin{leftbubbles}
    Left-aligned gray bubbles (241, 240, 240) with black text

    Right-aligned green bubbles (103, 184,104) with white text

    Bubbles only break after a paragraph (equivalent to an enter press when chatting). Long message with multiple lines will be kept in one bubble.

    Left and right edges are round.
\end{leftbubbles}

\begin{rightbubbles}
    Single
 \end{rightbubbles}


\end{document}

在此处输入图片描述

答案1

您需要更改\bubble命令中的设置。tcolorbox提供了一个选项,将(的底层命令)tcbox width=auto limited的宽度设置为自动,\tcbox\bubbleIE它遵循内容的宽度,但有最大值限制。要设置最大值,您可以使用width=\columnwidth选项。因此,更改气泡的定义就足够了:

\newcommand{\bubble}[4]{%
  \tcbox[
    on line,
    arc=4.5mm,
    colback=#1,
    colframe=#1,
    tcbox width=auto limited, % <-- added
    width=\columnwidth, % <-- added
    #2,
  ]{\color{#3}#4}%
}

输出:

在此处输入图片描述

相关内容