自动生成带有文本的 Tex 气泡

自动生成带有文本的 Tex 气泡

我正在做一个项目,需要大量的时间轴字段和所有跟踪内容的东西,所以我试图通过自动化让这一切变得更容易。我已经取得了很大的进展,但是有一个问题我无法解决,所以我寻求帮助。我创建了这个文档来展示它。我对格式表示歉意,我不熟悉如何在 tex.stackexchange 上正确格式化它。

\documentclass{article}
\usepackage{calc}
\usepackage{tikz}
\usepackage{xstring}
\usepackage{etoolbox}

\newcommand{\drawbubble}[3]{
    \renewcommand{\rowlng}{\arabic{rowlng}}
    \renewcommand{\rowh}{\arabic{nrrows}}
    \node at (#1-0.9,#2) {#3 };
    %\StrLen{#3}[\lenstr]
    \draw [rounded corners, thick, black] (#1-\rowlng/11-0.08,#2) -- (#1-\value{rowlng}/11-0.08,#2-0.6) -- (#1+\rowlng/11+0.08,#2-0.6) -- (#1+\rowlng/11+0.08,#2+0.6) -- (#1-\rowlng/11-0.08,#2+0.6) -- (#1-\rowlng/11-0.08,#2);}

%\newcommand{\drawbubble}[3]{\StrLen{#3}[\lenstr] 
%\draw [rounded corners, thick, black] (#1-\lenstr/11-0.08,#2) -- (#1-\lenstr/11-0.08,#2-0.6) -- (#1+\lenstr/11+0.08,#2-0.6) -- (#1+\lenstr/11+0.08,#2+0.6) -- (#1-\lenstr/11-0.08,#2+0.6) -- (#1-\lenstr/11-0.08,#2);
%\node at (#1,#2) {#3};}

\newcommand{\tmtxt}{}
\newcommand{\rowlng}{0}
\newcommand{\rowh}{0}
\newcounter{nrrows}
\newcounter{rowlng}
\newcounter{tstlng}
\newcommand{\addtxt}[2]{
    \appto#1{#2}
}

\makeatletter % we need to use kernel commands
\newcommand{\starttimetext}{%
    \renewcommand{\tmtxt}{\begin{tabular}[c]{@{}c@{}}}
        \setcounter{nrrows}{0}
        \setcounter{rowlng}{0}
        \setcounter{tstlng}{0}
        \@timetexti
    }
    \newcommand\@timetexti{\@ifnextchar\stoptimetext{\@timetextend}{\@timetextii}}

    \newcommand\@timetextii[1]{%
        \@timetextiii{#1}
        \stepcounter{nrrows}
        \@timetexti % restart the recursion
    }
    \newcommand\@timetextiii[1]{%
        \StrLen{#1}[\testlen]
        \setcounter{tstlng}{\testlen}
        \ifnum\therowlng<\thetstlng
        \setcounter{rowlng}{\value{tstlng}}
        \fi
        \addtxt{\tmtxt}{#1\\}}
    \newcommand\@timetextend[1]{% The argument is \stopimages
        \addtxt{\tmtxt}{\end{tabular}}
    %\arabic{rowlng}:\arabic{nrrows}
    \tmtxt
}
\makeatother

\begin{document}
\begin{tikzpicture}
\drawbubble{0}{4.2}{\starttimetext
    {test}{altso}
    \stoptimetext}
\drawbubble{0}{1.2}{\starttimetext
    {vaginas}
    \stoptimetext}
\drawbubble{3}{4.2}{\starttimetext
    {testingtonomatic}
    \stoptimetext}
\end{tikzpicture}
\end{document}

第一个有两条线,超出了边界,而后一个只有一行,没有超出边界。当我改变它们时,发生了同样的事情和其他事情。我试图让它计算线和宽度,并使周围的区域进行补偿。

我做了什么导致它在添加更多行时开始移动? timetext 部分是为了使行正常工作,否则我无法获得它。

答案1

我不确定您想要实现什么结果。您想要这样的结果吗?

可能性

\documentclass{article}
\usepackage{calc}
\usepackage{tikz}
\usepackage{xstring}
\usepackage{etoolbox}

\newcommand{\drawbubble}[3]{%
%   \renewcommand{\rowlng}{\arabic{rowlng}}%
%   \renewcommand{\rowh}{\arabic{nrrows}}%
  \node [draw, rounded corners, thick, black] at (#1-0.9,#2) {#3 };
  %\StrLen{#3}[\lenstr]
%   \draw [rounded corners, thick,  blue] (#1-\value{rowlng}/11-0.08,#2) -- (#1-\value{rowlng}/11-0.08,#2-0.6) -- (#1+\value{rowlng}/11+0.08,#2-0.6) -- (#1+\value{rowlng}/11+0.08,#2+0.6) -- (#1-\value{rowlng}/11-0.08,#2+0.6) -- (#1-\value{rowlng}/11-0.08,#2);
}

%\newcommand{\drawbubble}[3]{\StrLen{#3}[\lenstr]
%\draw [rounded corners, thick, black] (#1-\lenstr/11-0.08,#2) -- (#1-\lenstr/11-0.08,#2-0.6) -- (#1+\lenstr/11+0.08,#2-0.6) -- (#1+\lenstr/11+0.08,#2+0.6) -- (#1-\lenstr/11-0.08,#2+0.6) -- (#1-\lenstr/11-0.08,#2);
%\node at (#1,#2) {#3};}

\newcommand{\tmtxt}{}
% \newcommand{\rowlng}{0}
% \newcommand{\rowh}{0}
\newcounter{nrrows}
\newcounter{rowlng}
\newcounter{tstlng}
\newcommand{\addtxt}[2]{%
  \appto#1{#2}%
}

\makeatletter % we need to use kernel commands
\newcommand{\starttimetext}{%
\renewcommand{\tmtxt}{%
  \begin{tabular}[c]{@{}c@{}}%
  }%
  \setcounter{nrrows}{0}%
  \setcounter{rowlng}{0}%
  \setcounter{tstlng}{0}%
  \@timetexti
}
\newcommand\@timetexti{\@ifnextchar\stoptimetext{\@timetextend}{\@timetextii}}

\newcommand\@timetextii[1]{%
  \@timetextiii{#1}%
  \stepcounter{nrrows}%
  \@timetexti % restart the recursion
}
\newcommand\@timetextiii[1]{%
  \StrLen{#1}[\testlen]%
  \setcounter{tstlng}{\testlen}%
  \ifnum\therowlng<\thetstlng
    \setcounter{rowlng}{\value{tstlng}}%
  \fi
  \addtxt{\tmtxt}{#1\\}%
}
\newcommand\@timetextend[1]{% The argument is \stopimages
  \addtxt{\tmtxt}{\end{tabular}}%
  %\arabic{rowlng}:\arabic{nrrows}
  \tmtxt
}
\makeatother

\begin{document}
\begin{tikzpicture}
  \drawbubble{0}{4.2}{\starttimetext
    {test}{altso}
    \stoptimetext}
  \drawbubble{0}{1.2}{\starttimetext
    {vaginas}
    \stoptimetext}
  \drawbubble{3}{4.2}{\starttimetext
    {testingtonomatic}
    \stoptimetext}
\end{tikzpicture}
\end{document}

请注意,我已注释掉大部分(希望是全部)我认为是虚假的空格。如果您想要这些空格,则需要将它们放回原处。

相关内容