我正在尝试用 把三个小盒子放在一个大盒子里tikz
。三个小盒子并排放在大盒子的底部。
到目前为止我已经尝试过:
\documentclass[conference]{IEEEtran}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc,positioning}
\begin{document}
\begin{tikzpicture}[block/.style={draw, fill=white, rectangle, minimum width=0.4*\columnwidth, anchor=south}, font=\small]
\node[block, text width=0.1*\columnwidth, align=left](acc) at (0,0){Accelerometer};
\node[block, text width=0.1*\columnwidth, align=left](gyro) at (acc.east+0.1,0){Gyroscope};
\node[block, text width=0.1*\columnwidth, align=left](mag) at (gyro.east+0.1,0){Magnetometer};
\draw(0, 0)rectangle(0.4*\columnwidth, 0.4*\columnwidth);
\end{tikzpicture}
\end{document}
它甚至无法被编译。
- 哪里错了?我该如何修改?
- 为什么无法
text width=0.1*\columnwidth
改变盒子的大小?
答案1
没有 TikZ:
\documentclass[conference]{IEEEtran}
\usepackage{lipsum}
\newcommand{\innerwidth}[1]{%
\dimexpr(\columnwidth-\numexpr#1+1\relax\fboxsep)/#1\relax
}
\begin{document}
\lipsum[2]
\begin{center}
\framebox[\columnwidth][s]{%
\framebox[\innerwidth{3}]{Accelerometer\strut}\hfil
\framebox[\innerwidth{3}]{Gyroscope\strut}\hfil
\framebox[\innerwidth{3}]{Magnetometer\strut}%
}
\end{center}
\lipsum[2]
\begin{center}
\framebox[\columnwidth][s]{%
\framebox[\innerwidth{2}]{Something\strut}\hfil
\framebox[\innerwidth{2}]{Something else\strut}%
}
\end{center}
\lipsum[2]
\end{document}
具有更简单的界面:
\documentclass[conference]{IEEEtran}
\usepackage{xparse,lipsum}
\ExplSyntaxOn
\NewDocumentCommand{\boxes}{m}
{
\begin{center}
\framebox[\columnwidth][s]
{ \perf_inner_boxes:n { #1 } \unskip }
\end{center}
}
\seq_new:N \l_perf_texts_seq
\dim_new:N \l_perf_texts_dim
\cs_new_protected:Npn \perf_inner_boxes:n #1
{
\seq_set_split:Nnn \l_perf_texts_seq { , } { #1 }
\dim_set:Nn \l_perf_texts_dim
{
(
\columnwidth
-
\int_eval:n { 1 + \seq_count:N \l_perf_texts_seq }\fboxsep
)
/ \seq_count:N \l_perf_texts_seq
}
\seq_map_inline:Nn \l_perf_texts_seq
{
\framebox[\l_perf_texts_dim]{##1\strut}\hfil
}
}
\ExplSyntaxOff
\begin{document}
\lipsum[2]
\boxes{Accelerometer,Gyroscope,Magnetometer}
\lipsum[2]
\boxes{Something, Something else}
\lipsum[2]
\boxes{1,2,3,4,5,6,7,8,9,10}
\end{document}
答案2
你的代码有几个错误。这是更正后的版本。你可以使用fit
库来绘制外部矩形。
\documentclass[conference]{IEEEtran}
\usepackage{tikz}
\usepackage{showframe,lipsum} %% just for demo
\usetikzlibrary{backgrounds,calc,positioning,fit}
\begin{document}
\noindent
\begin{tikzpicture}[block/.style={draw, fill=white, rectangle, anchor=south}, minimum width={width("Magnetometer")+2pt},font=\small]
\node[block, align=left](acc) at (0,0){Accelerometer\vphantom{g}};
\node[block, align=left,right =3em of acc](gyro) {Gyroscope};
\node[block, align=left,right =3em of gyro](mag) {Magnetometer};
\node[draw,rectangle,fit=(acc) (gyro)(mag)] {};
\end{tikzpicture}
\lipsum
\end{document}
如果您希望框占据整个水平空间,请执行以下操作:
\documentclass[conference]{IEEEtran}
\usepackage{tikz}
\usepackage{showframe,lipsum} %% just for demo
\usetikzlibrary{backgrounds,calc,positioning,fit}
\begin{document}
\noindent
\begin{tikzpicture}[block/.style={draw, fill=white, rectangle, anchor=south}, minimum width=.32\columnwidth,font=\small]
\node[block, align=left](acc) at (0,0){Accelerometer\vphantom{g}};
\node[block, align=left,anchor=west](gyro) at (acc.east) {Gyroscope};
\node[block, align=left,anchor=west](mag) at (gyro.east) {Magnetometer};
\node[draw,rectangle,fit=(acc) (gyro)(mag)] {};
\end{tikzpicture}
\lipsum
\end{document}