使用 tikz 绘制 MongoDB 文档

使用 tikz 绘制 MongoDB 文档

我正在尝试重现

在此处输入图片描述

我曾尝试过

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{positioning}

\begin{document}

\begin{verbatim}
{
    _id: <ObjectId1>,
    username: "123xyz"
}
\end{verbatim}

\begin{verbatim}
{
    _id: <ObjectId2>,
    user_id: <ObjectId1>,
    phone: "123-456-7890",
    email: "[email protected]"
}
\end{verbatim}

\begin{verbatim}
{
    _id: <ObjectId3>,
    user_id: <ObjectId1>,
    level: 5,
    group: "dev"
}
\end{verbatim}

\begin{tikzpicture}[document/.style={draw, inner sep=10pt}]
\node [document] (user) {user document};
\node [document, above right=of user] (contact) {contact document};
\node [document, below right=of user] (access) {access document};

\draw[<->] (user) -- (contact);
\draw[<->] (user) -- (access);
\end{tikzpicture}


\end{document}

产生

在此处输入图片描述

这与预期结果相差甚远 :D 但我就是不明白如何将逐字框放入节点中。此外,由于我需要从逐字中的特定行绘制线条,我想我可能会将框中的每一行绘制为单独的节点?

答案1

这里有一个用于帧对齐的解决方案 listings和环境tabular

在此处输入图片描述

\documentclass{article}
\usepackage{listings}
\usepackage{array}
\usepackage{tikz}

\lstset{basicstyle=\ttfamily,
        escapechar=!}

\def\tikzmark#1{\tikz[overlay,remember picture]\node[inner sep=0pt,
 anchor=base](#1){\strut};}        

\begin{document}

\newsavebox{\lstone}
\newsavebox{\lsttwo}
\newsavebox{\lstthree}


\begin{lrbox}{\lstone}
\begin{lstlisting}
{
  _id: <ObjectId1>,!\tikzmark{A}!
  username: "123xyz"
}
\end{lstlisting}
\end{lrbox}

\begin{lrbox}{\lsttwo}
\begin{lstlisting}
{
  _id: <ObjectId2>,
  !\tikzmark{B}!user_id: <ObjectId1>,!\tikzmark{C}!
  phone: "123-456-7890",
  email: "[email protected]"
}
\end{lstlisting}
\end{lrbox}

\begin{lrbox}{\lstthree}
\begin{lstlisting}
{
  _id: <ObjectId3>,
  !\tikzmark{D}!user_id: <ObjectId1>,!\tikzmark{E}!   !!       
  level: 5,
  group: "dev"
}
\end{lstlisting}
\end{lrbox}



\begin{tabular}{m{4cm}!{\hspace{2cm}}m{6cm}}
user document \newline
\fbox{\usebox{\lstone}}& 
contact document \newline
\fbox{\usebox{\lsttwo}}\vskip1cm
access document \newline
\fbox{\usebox{\lstthree}}
\end{tabular}


\begin{tikzpicture}[remember picture,overlay,>=stealth]
\fill[green!20,opacity=.3] (B.south west)rectangle(C.north east);
\fill[green!20,opacity=.3] (D.south west)rectangle(E.north east);
\draw[<->,thick,green](A)--(B);
\draw[<->,thick,green](A)--(D);
\end{tikzpicture}

\end{document}

相关内容