使用 Latex 绘制并发图

发布于 2024-08-06 17:09:25 字数 257 浏览 1 评论 0原文

我希望能够在我正在上的课程的作业中画出一个漂亮的并发图。 Ascii 版本如下所示。有什么方法可以使用乳胶来做到这一点,使它看起来非常漂亮和干净吗?

<代码> 线程 A ----|=====read(7)=====|---------------------------- -------->
线程B -------------|======写(-3)=====|-----|===读(-7)=== |--->

或者至少是这样的。

I would like to be able to draw a nice concurrency diagram in my homework for a class I am taking. An ascii version would look something like the following. Is there any way to do this using latex that would make it look really nice and clean?


Thread A ----|=====read(7)=====|----------------------------------->
Thread B -------------|======write(-3)=====|-----|===read(-7)===|--->

Or at least something like this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆追雨的时光 2024-08-13 17:09:25

有相当多的绘图语言集成到 tex 中。

正如评论中所建议的,Dia,至少在 Linux 上,可以转储到 Metapost 代码,该代码可以被处理并插入到你的 tex 文档中。

您还可以从您选择的图形程序创建图形文件(jpg、png、pdf、eps),并仅包含该图形文件。这可能是最简单的路线。

在我的脑海中,与 Latex 兼容的绘图语言(非详尽)是:
前列腺素F,
蒂克兹,
MetaPost

TIKZ 和 PGF 非常强大,您可以在 texample.net 查看大量示例。

您还可以使用 graphviz,以及可选的 dot2tex,它将把您的 graphviz 文件转换为 Latex 可以理解的代码。

There are a quite a few drawing languages that integrate into tex.

As suggested in the comment, Dia, at least on linux, can dump to metapost code, which can be processed and inserted in your tex document.

You can also create a graphics file (jpg, png, pdf, eps) from your graphics program of choice, and just include that graphics file. This is probably the easiest route to go.

Off the top of my head, the drawing languages compatible with latex (non-exhaustive) are:
PGF,
TIKZ,
MetaPost

TIKZ and PGF are pretty powerful, and you can check out a bunch of examples at texample.net

You could also use graphviz, and optionally dot2tex, which will translate your graphviz file into code latex can understand.

兰花执着 2024-08-13 17:09:25

The timing macros for PGF/TikZ look like they could be used to do what you want.

哆啦不做梦 2024-08-13 17:09:25

是的,这是一个老问题,但这样的图在今天仍然有用。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, positioning}

\begin{document}

\resizebox{\textwidth}{!}{%
        \begin{tikzpicture}

            \draw [line width=0.3mm] (0, 0) -- (16, 0);
            \draw [line width=0.3mm] (0, -1) -- (16, -1);
            \draw [line width=0.3mm] (0, -1 * 2) -- (16, -1 * 2);
            \draw [line width=0.3mm] (0, -1 * 3) -- (16, -1 * 3);

            \draw [line width=1mm, {Square}-{Square}, gray] (1, 0) -- (3, 0)  node[midway,above] {Thread 1};
            \draw [line width=1mm, {Square}-{Square}] (3, -1) -- (5, -1) node[midway,above] {Thread 2};
            \draw [line width=1mm, {Square}-{Square}] (5, -1 * 2) -- (7, -1 * 2);
            \draw [line width=1mm, {Square}-{Square}] (7, -1 * 3) -- (9, -1 * 3);        
  
        \end{tikzpicture}
}

\end{document}

输入图片此处描述

Yeah, this is an old question, but such diagram is still useful today.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, positioning}

\begin{document}

\resizebox{\textwidth}{!}{%
        \begin{tikzpicture}

            \draw [line width=0.3mm] (0, 0) -- (16, 0);
            \draw [line width=0.3mm] (0, -1) -- (16, -1);
            \draw [line width=0.3mm] (0, -1 * 2) -- (16, -1 * 2);
            \draw [line width=0.3mm] (0, -1 * 3) -- (16, -1 * 3);

            \draw [line width=1mm, {Square}-{Square}, gray] (1, 0) -- (3, 0)  node[midway,above] {Thread 1};
            \draw [line width=1mm, {Square}-{Square}] (3, -1) -- (5, -1) node[midway,above] {Thread 2};
            \draw [line width=1mm, {Square}-{Square}] (5, -1 * 2) -- (7, -1 * 2);
            \draw [line width=1mm, {Square}-{Square}] (7, -1 * 3) -- (9, -1 * 3);        
  
        \end{tikzpicture}
}

\end{document}

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文