作为后续问题这个帖子我正在尝试找出如何在一个表中排列多个 tikspictures,其中我的图表由我自己的宏定义。
问题:单独显示图表是可行的,但它们不会加载到表格中。此外,我希望表格覆盖文档的文本宽度。
我的问题类似于这个不同之处在于我试图调用我在序言中定义的命令。
\documentclass[a4paper,11pt,twoside]{book}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{tabularx}
\newcommand{\clocks}[4]{
\begin{figure}[htb]
% \resizebox{#5 \textwidth}{!}{
\begin{tikzpicture}
\begin{axis}[
ybar,
% enlargelimits=0.25,
% legend style={at={(0.5,-0.15)},
% anchor=north,legend columns=-1},
ylabel={CPU clocks},
symbolic x coords={one, two, three, four},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
]
\addplot coordinates {
(one, #1)
(two, #2)
(three, #3)
(four, #4)};
%\legend{one, two, three, four}
\end{axis}
\end{tikzpicture}
%}
\end{figure}
}
\begin{document}
\clocks{20}{50}{30}{70}
\begin{tabularx}{\textwidth}{|X|X|}
\hline
A & B \\
\hline
C & D \\
\hline
\end{tabularx}
%this table should be scaled to pagewidth!
\begin{tabularx}{\textwidth}{|X|X|}
\hline
\clocks{20}{50}{30}{70} & \clocks{20}{50}{30}{70} \\
\hline
\clocks{20}{50}{30}{70} & \clocks{20}{50}{30}{70} \\
\hline
\end{tabularx}
\end{document}
答案1
figure
您不能在非浮动结构(例如)内使用浮动对象( );从的定义中tabularx
删除环境,并使用可选参数来控制宽度(我将默认值设置为,但可以使用您更喜欢的值):figure
\clock
\linewidth
\documentclass[a4paper,11pt,twoside]{book}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{tabularx}
\pgfplotsset{compat=newest}
\newcommand{\clocks}[5][\linewidth]{%
% \resizebox{#5 \textwidth}{!}{
\begin{tikzpicture}
\begin{axis}[
width=#1,
ybar,
% enlargelimits=0.25,
% legend style={at={(0.5,-0.15)},
% anchor=north,legend columns=-1},
ylabel={CPU clocks},
symbolic x coords={one, two, three, four},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
]
\addplot coordinates {
(one, #2)
(two, #3)
(three, #4)
(four, #5)};
%\legend{one, two, three, four}
\end{axis}
\end{tikzpicture}%
%}
}
\begin{document}
\noindent\begin{tabularx}{\textwidth}{|X|X|}
\hline
\clocks{20}{50}{30}{70} & \clocks{20}{50}{30}{70} \\
\hline
\clocks{20}{50}{30}{70} & \clocks{20}{50}{30}{70} \\
\hline
\end{tabularx}
\end{document}
抑制表中的行并使用enlargelimits
,结果会更清晰:
\documentclass[a4paper,11pt,twoside]{book}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{tabularx}
\pgfplotsset{compat=newest}
\newcommand{\clocks}[5][\linewidth]{%
\begin{tikzpicture}
\begin{axis}[
width=#1,
ybar,
enlargelimits=0.25,
% legend style={at={(0.5,-0.15)},
% anchor=north,legend columns=-1},
ylabel={CPU clocks},
symbolic x coords={one, two, three, four},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
]
\addplot coordinates {
(one, #2)
(two, #3)
(three, #4)
(four, #5)};
%\legend{one, two, three, four}
\end{axis}
\end{tikzpicture}%
%}
}
\begin{document}
\noindent\begin{tabularx}{\textwidth}{XX}
\clocks{20}{50}{30}{70} & \clocks{20}{50}{30}{70} \\
\clocks{20}{50}{30}{70} & \clocks{20}{50}{30}{70} \\
\end{tabularx}
\end{document}
在浮动物体外面,您可以使用或包\captionof
提供的 来提供标题(在评论中请求):caption
capt-of
\documentclass[a4paper,11pt,twoside]{book}
\usepackage{caption}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{tabularx}
\pgfplotsset{compat=newest}
\newcommand{\clocks}[5][\linewidth]{%
% \resizebox{#5 \textwidth}{!}{
\begin{tikzpicture}
\begin{axis}[
width=#1,
ybar,
enlargelimits=0.25,
% legend style={at={(0.5,-0.15)},
% anchor=north,legend columns=-1},
ylabel={CPU clocks},
symbolic x coords={one, two, three, four},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
]
\addplot coordinates {
(one, #2)
(two, #3)
(three, #4)
(four, #5)};
%\legend{one, two, three, four}
\end{axis}
\end{tikzpicture}%
%}
}
\begin{document}
\noindent\begin{tabularx}{\textwidth}{XX}
\clocks{20}{50}{30}{70}
\captionof{figure}{a description for the first plot}
&
\clocks{20}{50}{30}{70}
\captionof{figure}{a description for the second plot} \\
\clocks{20}{50}{30}{70}
\captionof{figure}{a description for the third plot}
& \clocks{20}{50}{30}{70}
\captionof{figure}{a description for the fourth plot} \\
\end{tabularx}
\end{document}
答案2
(!)\noindent
在表格前使用以避免过度警告。
使用\resizebox
而不是figure
。设置其值。我正在使用.45\textwidth
。
\newcommand{\clocks}[4]{
\resizebox{.45\textwidth}{!}{\centering
\begin{tikzpicture}
\begin{axis}[
ybar,
ylabel={CPU clocks},
symbolic x coords={one, two, three, four},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
]
\addplot coordinates {
(one, #1)
(two, #2)
(three, #3)
(four, #4)};
\end{axis}
\end{tikzpicture}
}
}
\noindent\begin{tabularx}{\textwidth}{|X|X|}
\hline
\clocks{20}{50}{30}{70} & \clocks{20}{50}{30}{70} \\
\hline
\clocks{20}{50}{30}{70} & \clocks{20}{50}{30}{70} \\
\hline
\end{tabularx}