我创建了一个简单的图表。
\begin{figure}[t]
\begin{center}
\setlength{\unitlength}{10pt}
\begin{picture}(24,4)
\setlength\fboxsep{0pt}
\put(0,2){$\underbrace{\strut \fcolorbox{black}{teal0}{\framebox(6,2){Preamble}}\fcolorbox{black}{purp0}{\framebox(7,2){Start code}}}_\textrm{Stage 1: estimate M}$}
\put(13,2){$\underbrace{\strut \fcolorbox{black}{lime0}{\framebox(5,2){Data}}\fcolorbox{black}{orange0}{\framebox(6,2){Stop code}}}_\textrm{Stage 2: sample and decode}$}
\end{picture}
\caption{Packet format.}
\label{fig:packet}
\end{center}
\end{figure}
并创建以下内容:
但是,如果仔细观察,您会发现最后一个框中的文本与其他框中的文本不对齐,这是因为“p”的原因。我想将其对齐。解决方案应尽可能接近原始图像,并且不需要 TikZ 等附加软件包。
答案1
请始终发布显示所有使用过的包装(和颜色)的完整文件。我制作了一些不同的颜色。
无论如何,由于您处于图片模式,因此使用\put
坐标直接控制括号的位置更容易,并且可以使用隐藏文本的深度\raisebox.
\documentclass{article}
\usepackage{color}
\begin{document}
\begin{figure}[t]
\begin{center}
\setlength{\unitlength}{10pt}
\begin{picture}(24,4)
\setlength\fboxsep{0pt}
\put(0,2){\fcolorbox{black}{red}{\framebox(6,2){Preamble}}%
\fcolorbox{black}{blue}{\framebox(7,2){Start code}}%
\fcolorbox{black}{yellow}{\framebox(5,2){Data}}%
\fcolorbox{black}{green}{\framebox(6,2){\raisebox{0pt}[\height][0pt]{Stop code}}}}
\put(0,0){\makebox(13.2,2){\upbracefill}\makebox(11.2,2){\upbracefill}}
\put(0,-1){\makebox(13.2,2){Stage 1: estimate M}%
\makebox(11.2,2){Stage 2: sample and decode}}
\end{picture}
\caption{Packet format.}
\label{fig:packet}
\end{center}
\end{figure}
\end{document}
答案2
这全都与以下位置有关\strut
:
\documentclass{article}
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\begin{document}
\begin{center}
\setlength{\unitlength}{10pt}
\begin{picture}(24,4)
\setlength\fboxsep{0pt}
\put(0,2){$\underbrace{\strut \fcolorbox{black}{red}{\framebox(6,2){Preamble}}\fcolorbox{black}{blue}{\framebox(7,2){Start code}}}_\textrm{Stage 1: estimate M}$}%
\put(13,2){$\underbrace{\strut \fcolorbox{black}{green}{\framebox(5,2){Data}}\fcolorbox{black}{yellow}{\framebox(6,2){Stop code}}}_\textrm{Stage 2: sample and decode}$}
\end{picture}
\end{center}
\begin{center}
\setlength{\unitlength}{10pt}
\begin{picture}(24,4)
\setlength\fboxsep{0pt}
\put(0,2){$\underbrace{\fcolorbox{black}{red}{\framebox(6,2){\strut Preamble}}\fcolorbox{black}{blue}{\framebox(7,2){\strut Start code}}}_\textrm{Stage 1: estimate M}$}%
\put(13,2){$\underbrace{\fcolorbox{black}{green}{\framebox(5,2){\strut Data}}\fcolorbox{black}{yellow}{\framebox(6,2){\strut Stop code}}}_\textrm{Stage 2: sample and decode}$}
\end{picture}
\end{center}
\end{document}
上图是原始输出,下图\strut
在每个框中使用了。由于您使用的是\fcolorbox
,因此您可以创建类似
\newcommand{\paddedbox}[4][1ex]{%
{\setlength{\fboxsep}{#1}\fcolorbox{#2}{#3}{\strut\quad#4\quad}}}%
它创建一个填充框(通过设置\fboxsep
;默认值为1ex
),垂直和水平(通过额外放置\quad
s(1em
空格)。代码更简洁一些
\begin{center}
\setlength{\unitlength}{10pt}
\begin{picture}(24,4)
\put(0,2){$\underbrace{\paddedbox{black}{red}{Preamble}\paddedbox{black}{blue}{Start code}}_\textrm{Stage 1: estimate M}$}%
\put(15,2){$\underbrace{\paddedbox{black}{green}{Data}\paddedbox{black}{yellow}{Stop code}}_\textrm{Stage 2: sample and decode}$}
\end{picture}
\end{center}
并产生
答案3
作为参考,TikZ 的相同问题可以按照以下方法修复TikZ 和节点内文本的垂直对齐问题。
结合这个提示在 TikZ 中使用括号连接节点,可以构建以下解决方案:
\documentclass{article}
\usepackage{tikz,pdftexcmds}
\usetikzlibrary{calc,shapes.arrows,decorations.pathreplacing,shadows}
% list of keys
\pgfkeys{/tikz/.cd,
border color/.initial=black,
border color/.get=\bordercol,
border color/.store in=\bordercol,
brace color/.initial=black,
brace color/.get=\bracecol,
brace color/.store in=\bracecol,
brace distance/.initial=5pt,
brace distance/.get=\bracedistance,
brace distance/.store in=\bracedistance,
brace position/.initial=north,
brace position/.get=\bracepos,
brace position/.store in=\bracepos,
items height/.initial=1cm,
items height/.get=\itemheight,
items height/.store in=\itemheight,
items width/.initial=3cm,
items width/.get=\itemwidth,
items width/.store in=\itemwidth,
items distance/.initial=\itemwidth,
items distance/.get=\itemdistance,
items distance/.store in=\itemdistance,
}
% list of styles
\tikzset{module/.style={
draw=\bordercol,
minimum height=\itemheight,
minimum width=\itemwidth,
text height=1.5ex,
text depth=.25ex,
},
brace/.style={
decoration={brace,raise=\bracedistance,amplitude=0.75em,#1},
decorate,
draw=\bracecol,
very thick,
}
}
\makeatletter
\newcommand{\braceddiagram}[3][]{
\begin{tikzpicture}[scale=0.9,transform shape,#1] % here we can pass options:
% scale=0.9,transform shape are used to avoid overfull and underfull boxes
% counting all the items to be displayed
\foreach \items/\colors [count=\xi] in {#2}{\global\let\maxnumitem\xi}
% foreach item we are going to compute at which distance it should be located
% and then we put it with the module style and with a name <module><num-module>
\foreach \items/\colors [count=\xi] in {#2}{%
\path let
\n1={0+\xi*\itemdistance} in
node[module,fill=\colors] (module\xi) at +(\n1,0) {\items};
}
% at the end we draw the brace with the comment
\ifnum\pdf@strcmp{\bracepos}{south}=\z@
\draw[brace=mirror] (module1.south west)--(module\maxnumitem.south east)
node[below=3*\bracedistance, midway]{#3};
\else
\draw[brace] (module1.north west)--(module\maxnumitem.north east)
node[above=3*\bracedistance, midway]{#3};
\fi
\end{tikzpicture}
}
\makeatother
\begin{document}
Generic examples:
\braceddiagram[brace distance=4pt]{Bla/red, Bla/blue, Bla/green}{Some comment}
\vspace{1cm}
\tikzset{my style for the diagram/.style={
brace distance=6pt,
brace position=south,
}
}
\braceddiagram[my style for the diagram]{Bla/red, Bla/blue, Bla/green,Bla/yellow}{Some other comment}
\vspace{1cm}
The real example:
\begin{center}
\braceddiagram[my style for the diagram]{Preamble/red, Start code/blue}{Stage 1: estimate M}
\braceddiagram[my style for the diagram]{Data/green,Stop code/yellow}{Stage 2: sample and decode}
\end{center}
\end{document}
结果: