如何为一些文本、线条、图片创建解释,以及如何对齐文本

如何为一些文本、线条、图片创建解释,以及如何对齐文本

我可以创建一个颜色表,但现在我想为该表添加一些解释,如下图所示

在此处输入图片描述

我应该使用tikz还是只使用表格功能?有人能给我一个起点吗?

这是我的表格代码:

\documentclass[xcolor=x11names,compress]{beamer}

\usepackage{xcolor, colortbl}
\usepackage{makecell}

\definecolor{Gray}{gray}{0.85}

\begin{document} 

\begin{frame}{I$^{2}$C contd}
\begin{table}
\centering
\begin{tabular}{|c|c|c|c|c|c|c|c|c|}
\hline
\cellcolor{Gray}S & \cellcolor{Gray}SLAVE ADDRESS & \cellcolor{Gray}R/W  & A & \cellcolor{Gray}DATA & A & \cellcolor{Gray}DATA & A/A & \cellcolor{Gray}P \\
\hline
\end{tabular}
\end{table}
\end{frame}

\end{document}

我找到了一种方法来做到这一点,这里是输出和代码:

\documentclass[xcolor=x11names,compress]{beamer}

\usepackage{xcolor}
\usepackage{tikz}
\usepackage{amsmath,amssymb}

\definecolor{Gray}{gray}{0.85}

\begin{document} 

\begin{frame}{I$^{2}$C contd}
\begin{figure}
\centering
\hspace*{-1.5em}
\begin{tikzpicture}
    \draw[gray] (0,0) grid (12,6);
    \foreach \x in {0,1,...,12}
            \draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
    \foreach \y in {1,2,...,6}
        \draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\y$};

    %draw data structure
    \draw [fill=Gray] (0,5.5)    rectangle (1,6)    node[pos=.5] {S};
    \draw [fill=Gray] (1,5.5)    rectangle (4.5,6)  node[pos=.5] {SLAVE ADDRESS};
    \draw [fill=Gray] (4.5,5.5)  rectangle (5.5,6)  node[pos=.5] {R/$\overline{W}$};
    \draw             (5.5,5.5)  rectangle (6,6)    node[pos=.5] {A};
    \draw [fill=Gray] (6,5.5)    rectangle (7.5,6)  node[pos=.5] {DATA};
    \draw             (7.5,5.5)  rectangle (8,6)    node[pos=.5] {A};
    \draw [fill=Gray] (8,5.5) rectangle (9.5,6) node[pos=.5] {DATA};
    \draw             (9.5,5.5) rectangle (11,6) node[pos=.5] {A/$\overline{A}$};
    \draw [fill=Gray] (11,5.5) rectangle (11.5,6) node[pos=.5] {P};

    %draw example
    \draw [fill=Gray] (0.5,3) rectangle (1,3.5);
    \draw             (0.5,2) rectangle (1,2.5);

    %draw line
    \draw (5,5.3) -- (5,4.5);
    \draw (6,5.3) -- (6,4.5) -- (6.5,4.5);
    \draw (11,5.3) -- (11,4.5) -- (10.5,4.5);

    %add text
    %\coordinate [label={[blue]above:`0'(write)}] (RW) at (5,4);
    %\coordinate [label={[blue]above:`0'(data transferred \\())}] (RW) at (8.5,4);
    \node [blue, align=center, above] at (5,4) {`0'(write)};
    \node [blue, align=center, above] at (8.5,4) {data transferred \\(n bytes + ACK)};
    \node [red, align=left, above] at (2.8,3) {from master to slave};
    \node [red, align=left, above] at (2.8,2) {from slave to master};
    \node [red, align=left, above] at (8, 3) {A$=$ACK (SDA LOW)};
    \node [red, align=left, above] at (8, 2.5) {$\overline{A}=$NAK (SDA HIGH)};
    \node [red, align=left, above] at (7, 2) {S=START};
    \node [red, align=left, above] at (7, 1.5) {P=STOP};


\end{tikzpicture}
\end{figure}
\end{frame}

\end{document}

它的输出如下: 在此处输入图片描述 但是,红色文本有什么问题?我已经使用了align=leftrightcenter,它们的输出都相同。

我把代码改成:

\node [red, align=left, above] at (2.8,3) {from master to slave};
\node [red, align=left, above] at (2.8,2) {from slave to master};

它可以输出以上两个正确的内容,但是为什么呢?正确的文本怎么样,如何对齐它们?

答案1

我个人会使用并根据条件来制作方形节点chains以进行填充。

输出

在此处输入图片描述

代码

\documentclass[xcolor=x11names,compress]{beamer}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{amsmath,amssymb}


\usetikzlibrary{calc, positioning, chains}

\tikzset{
    box/.style={draw, text centered, minimum size=2em},
    sqarr/.style={to path={-- ++(0,-8mm)  -| (\tikztotarget) \tikztonodes},pos=.25}
}

\begin{document}
\begin{frame}{I$^{2}$C contd}
\begin{figure}
\centering
\hspace*{-1.5em}
\begin{tikzpicture}[font=\sffamily, shorten <= 1mm, shorten >= 1mm]
\begin{scope} [start chain, node distance=-\pgflinewidth, every node/.style={fill=gray!50}]  
    \foreach \name [count=\xi starting from 0] in {%
        S,
        SLAVE ADDRESS, 
        R/\={W},
        A,
        DATA, 
        A,
        DATA,
        A/\={A},
        P}{%
        \pgfmathparse{\xi>2 && mod(\xi,2)}
        \ifnum\pgfmathresult=0
            \node[box, on chain] (n\xi) {\name\vphantom{R/\={W}}};
        \else
            \node[box, fill=white, on chain] (n\xi) {\name\vphantom{R/\={W}}};
        \fi
    }
\end{scope}

\draw (n2.south) --++ (0,-8mm) node[below, font=\footnotesize] {'0' (write)};
\draw[sqarr] (n3.south east) to node[text width=2.5cm, yshift=-.3\baselineskip, inner xsep=0, text centered, fill=white, font=\footnotesize] {data transferred \\(n bytes + ACK)} (n7.south west);

\node[box, font=\footnotesize, fill=gray!50, minimum size=1em, below=2cm of n0, label={right:from master to slave}] (d1) {};
\node[box, minimum size=1em, below=2.5cm of n0, label={right:from slave to master}] {};

\node[text width=4.5cm,font=\footnotesize, anchor=north west] at (d1.north-|n3) {
        A = acknowledge (SDA LOW)\\
        \={A} = acknowledge (SDA HIGH)\\
        S = START condition\\
        P = STOP condition

}; 
\end{tikzpicture}   
\end{figure}
\end{frame}
\end{document}

答案2

最简单的方法可能是将所有线条放在同一个节点内,并左对齐。这就是我在下面的代码中所做的,它还展示了如何使用nodes节点rectangle+node绘制框架。

\documentclass[xcolor=x11names,compress]{beamer}

\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{amsmath,amssymb}

\definecolor{Gray}{gray}{0.85}

\begin{document} 

\begin{frame}[fragile]{I$^{2}$C contd}
Follow figure shows a master writes data to slave with 7-bits address:
\medskip
{\par\centering
\begin{tikzpicture}[field/.style={draw, minimum height=5mm, outer sep=-\pgflinewidth, fill={#1}, inner ysep=0pt},
    field/.default={Gray},]

    \begin{scope}[node distance=0pt]
    \node[field, minimum width=1cm] (S) {S};
    \node[field, right=of S] (SA) {SLAVE ADDRESS};
    \node[field, right=of SA, label={[label distance=5mm, blue, name=label1]-90:0 (write)}] (RW) {R/$\overline{\text{W}}$};
    \node[field=white, right=of RW] (A1) {A};
    \node[field, right=of A1] (D1) {DATA};
    \node[field=white, right=of D1] (A2) {A};
    \node[field, right=of A2] (D2) {DATA};
    \node[field=white, right=of D2] (AA) {A/$\overline{\text{A}}$};
    \node[field, right=of AA] (P) {P};
    \end{scope}
    \draw[shorten <=1mm, shorten >=0mm] (RW)--(label1);
    \draw[shorten <=1mm, shorten >=1mm] (A1.south east)--(A1.south east|-label1)-|(AA.south east) node[pos=.25, align=center, blue, fill=white] {data transferred \\(n bytes + ACK)};

    \node[draw, fill=Gray, minimum size=5mm, label={[name=label2]0:from master to slave}, below=1.5cm of S] (fmts) {};
    \node[draw, minimum size=5mm, label=0:from slave to master, below= 5mm of fmts] (fstm) {};
    \node [align=left, anchor=north west] at (fmts.north-|A1.west) {A = ACK (SDA LOW)\\$\overline{\text{A}}$ = NACK (SDA HIGH)\\S = START\\P = STOP};
\end{tikzpicture}\par}
\end{frame}

\end{document}

在此处输入图片描述

相关内容