如何对齐 tikzpicture 和表格?

如何对齐 tikzpicture 和表格?

我需要让 tikzpicture 的高度与表格相同(位于 \toprule 和 \bottomrule 之间),并且位于图片中显示的位置。你知道我该如何做吗?

\documentclass[a4paper,10pt,titlepage,usenames,dvipsnames,draft]{book}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{braket}
\usepackage{graphicx}
\graphicspath{{Images/}}
\DeclareGraphicsExtensions{.pdf,.jpeg,.jpg,.png,.eps,.fig}
\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\vec}{\mathbf}
\usepackage{float}
\usepackage{subfig}
\usepackage{listings}
\usepackage{multirow}
\usepackage{caption}
\usepackage{booktabs}
\usepackage[dvipsnames]{xcolor}
\usepackage{xcolor}
\usepackage[T1]{fontenc}        
\usepackage{amsmath}
%\documentclass{standalone}
%\usetikzlibrary{external}
%\tikzexternalize
%\pgfplotsset{compat = newest}
%\usepackage{pgfplots}
%\pgfplotsset{compat=newest}
%\pagestyle{empty}
%\usepgfplotslibrary{patchplots}
\usepackage{fontawesome}
\usepackage{pdflscape}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{filecontents}
\usepackage{mathtools}
\usepackage{makeidx}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{graphicx}
\usepackage{cancel}
\usepackage{appendix}
\usepackage{eurosym}
\usepackage{setspace}
\usepackage{bm}
\usepackage{tabularx}
\usepackage{hyperref}
\usepackage{siunitx}
\geometry{a4paper,left=30mm, right=25mm,top=20mm,bottom=20mm}
\usepackage{titlesec}
\usepackage{wrapfig}
\begin{document}

\begin{table}[H]
\begin{tabular}{ccccccc}
\textbf{Categories} & \textbf{QR} &\textbf{weigh}t & \rotatebox{90}{\emph{Xiaomi}} & \rotatebox{90}{\emph{HONOR}} & \rotatebox{90}{\emph{HUAWEI}} & \rotatebox{90}{\emph{Fitbit}} \\ \toprule
\multirow{4}{*}{functionality} & lightness &  &  &  &  &  \\
 & durability &  &  &  &  &  \\
 & accuracy &  &  &  &  &  \\
 & battery life &  &  &  &  &  \\
safety & biocompatibility &  &  &  &  &  \\ \midrule
\multirow{7}{*}{comfort and usability} & surface softness &  &  &  &  &  \\ \midrule
 & grip &  &  &  &  &  \\
 & easiness &  &  &  &  &  \\
 & waterproof &  &  &  &  &  \\
 & washability &  &  &  &  &  \\
 & breathability &  &  &  &  &  \\
 & heat/cold resistance &  &  &  &  &  \\ \midrule
 design& style &  &  &  &  &  \\ \midrule
 \multirow{2}{*}{pleasure}& screen size &  &  &  &  &  \\
 & ecological &  &  &  &  & \\ \bottomrule
\end{tabular}
\end{table}



\begin{tikzpicture}
        \begin{axis}[
 %transpose legend,
     %legend columns=1,
     %legend style={at={(0.5,-0.1)},anchor=north},%%%
            xmin = 0, xmax = 6,
            ymin = 0, ymax = 16,
xtick=\empty, ytick=\empty,
            xtick distance = 1,
            ytick distance = 1,
     grid = both,
     %title= Relative angle,
            %minor tick num = 1,
            major grid style = {lightgray},
            minor grid style = {lightgray!25},
            width = 0.26\textwidth,
            height = 0.734\textwidth,
            %xlabel = {$x$},
            %ylabel = {$y$},
           % legend cell align = {left},
        ]
           % \addplot[
               % domain = 0:30,
                %samples = 200,
                %smooth,
                %thick,
                %blue,
            %] {exp(-x/10)*( cos(deg(x)) + sin(deg(x))/10 )};
             \addplot[
                %smooth,
                thick,
                red,
        mark=*,
                %dashed
            ] file[skip first] {xiaomi.dat};

\addplot[
                %smooth,
                thick,
                blue,
        mark=square,
                %dashed
            ] file[skip first] {honor.dat};
\addplot[
                %smooth,
                thick,
                green,
        mark=triangle,
                %dashed
            ] file[skip first] {huawei.dat};
\addplot[
                %smooth,
                thick,
                yellow,
        mark=x,
                %dashed
            ] file[skip first] {fitbit.dat};

            %\legend{Huawei}
        \end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

对于.dat文件,您可以使用相同的文件四次:

x         y
 2       0.5         
 4      1.5
 3      2.5
 2      3.5
 1      4.5
 1      5.5
 2      6.5
 3      7.5 
 4      8.5 
 2      9.5
 3      10.5    
 3      11.5
 4      12.5
 1      13.5
 3      14.5
 1      15.5

答案1

所需表格的一些近似值......

在此处输入图片描述

\documentclass[a4paper,10pt,titlepage,usenames,dvipsnames,draft]{book}
\usepackage[a4paper, 
            left=30mm, right=25mm,
            top=20mm,bottom=20mm]{geometry}
%\usepackage[T1]{fontenc}
%\usepackage[utf8]{inputenc}
\usepackage{rotating}
\usepackage{booktabs, makecell, multirow, tabularx}
    \renewcommand\theadfont{\small\bfseries}
\newsavebox{\tabox}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{filecontents}

\begin{filecontents*}{xiaomi.dat}
x         y
 2      0.5
 4      1.5
 3      2.5
 2      3.5
 1      4.5
 1      5.5
 2      6.5
 3      7.5
 4      8.5
 2      9.5
 3      10.5
 3      11.5
 4      12.5
 1      13.5
 3      14.5
 1      15.5
\end{filecontents*}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{table}[ht]
    \centering
\sbox\tabox{
    \settowidth\rotheadsize{\theadfont HUAWEI }
\begin{tabularx}{0.75\linewidth}[b]{@{} XX ccccc c @{}}
\thead{Categories} 
    & \thead{QR} 
        &\thead{weight}
            & \rothead{\emph{Xiaomi}} 
                & \rothead{\emph{HONOR}} 
                    & \rothead{\emph{HUAWEI}} 
                            & \rothead{\emph{Fitb}} \\
    \toprule
\multirow{4}{=}{functionality} 
    & lightness             &   &   &   &   &   \\
    & durability            &   &   &   &   &   \\
    & accuracy              &   &   &   &   &   \\
    & battery life          &   &   &   &   &   \\
safety 
    & biocompatibility      &   &   &   &   &   \\ 
    \cmidrule{1-7}
\multirow{7}{=}{comfort and usability} 
    & surface softness      &   &   &   &   &   \\ 
    \cmidrule{1-7}
    & grip                  &   &   &   &   &   \\
    & easiness              &   &   &   &   &   \\
    & waterproof            &   &   &   &   &   \\
    & washability           &   &   &   &   &   \\
    & breathability         &   &   &   &   &   \\
    & heat/cold resistance  &   &   &   &   &   \\ 
    \cmidrule{1-7}
design
    & style                 &   &   &   &   &   \\ 
    \cmidrule{1-7}
\multirow{2}{=}{pleasure}
    & screen size           &   &   &   &   &   \\
    & ecological            &   &   &   &   &   \\  
    \bottomrule
\end{tabularx}}
%
\usebox\tabox\begin{tikzpicture}[inner sep=0pt]
        \begin{axis}[
        xmin = 0, xmax = 6,
        ymin = 0, ymax = 16,
        width=44mm, height=\dimexpr\ht\tabox+\dp\tabox-\baselineskip,
        xticklabels={}, yticklabels={},
        grid=both,
        minor tick num = 4,
        major grid style = {lightgray},
        minor grid style = {lightgray!25},
                    ]
        \addplot +[thick] file[skip first] {xiaomi.dat};
        \end{axis}
            \end{tikzpicture}
\end{table}
\end{document}

图表的高度是通过\tabox存储表的位置(\ht\tabox + \hp\tabox)来测量的。对齐位于表的底部。

答案2

您可以使用 在表格中找到适当的点\tikzmark。您可以使用 PGF 宏计算两点之间的距离。最后,您可以将 tikzpicture 放入保存框并使用\resizebox(graphicx 包) 缩放它。在这种情况下,我使用了\includegraphics,因为提供的 pgfplot 不会运行。

3pt 和 2pt 是伪造的系数。我不想翻遍书单来查找实际值。

\documentclass[a4paper,10pt,titlepage,usenames,dvipsnames]{book}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{braket}
\usepackage{graphicx}
\graphicspath{{Images/}}
\DeclareGraphicsExtensions{.pdf,.jpeg,.jpg,.png,.eps,.fig}
\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\vec}{\mathbf}
\usepackage{float}
\usepackage{subfig}
\usepackage{listings}
\usepackage{multirow}
\usepackage{caption}
\usepackage{booktabs}
\usepackage[dvipsnames]{xcolor}
%\usepackage{xcolor}% duplicate
\usepackage[T1]{fontenc}        
%\usepackage{amsmath}% duplicate
%\documentclass{standalone}
%\usetikzlibrary{external}
%\tikzexternalize
%\pgfplotsset{compat = newest}
%\usepackage{pgfplots}
%\pgfplotsset{compat=newest}
%\pagestyle{empty}
%\usepgfplotslibrary{patchplots}
\usepackage{fontawesome}
\usepackage{pdflscape}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{filecontents}
\usepackage{mathtools}
\usepackage{makeidx}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{graphicx}
\usepackage{cancel}
\usepackage{appendix}
\usepackage{eurosym}
\usepackage{setspace}
\usepackage{bm}
\usepackage{tabularx}
\usepackage{hyperref}% should go last
\usepackage{siunitx}
\geometry{a4paper,left=30mm, right=25mm,top=20mm,bottom=20mm}
\usepackage{titlesec}
%\usepackage{wrapfig}% duplicate

\usetikzlibrary{tikzmark}
\newlength{\tempdima}

\begin{document}

\begin{table}[H]
\begin{tabular}{cccccccl@{}}
\textbf{Categories} & \textbf{QR} &\textbf{weigh}t & \rotatebox{90}{\emph{Xiaomi}} & \rotatebox{90}{\emph{HONOR}} & \rotatebox{90}{\emph{HUAWEI}} & \rotatebox{90}{\emph{Fitbit}} \\ \toprule
\multirow{4}{*}{functionality} & lightness &  &  &  &  & & \tikzmark{top}\\
 & durability &  &  &  &  &  \\
 & accuracy &  &  &  &  &  \\
 & battery life &  &  &  &  &  \\
safety & biocompatibility &  &  &  &  &  \\ \midrule
\multirow{7}{*}{comfort and usability} & surface softness &  &  &  &  &  \\ \midrule
 & grip &  &  &  &  &  \\
 & easiness &  &  &  &  &  \\
 & waterproof &  &  &  &  &  \\
 & washability &  &  &  &  &  \\
 & breathability &  &  &  &  &  \\
 & heat/cold resistance &  &  &  &  &  \\ \midrule
 design& style &  &  &  &  &  \\ \midrule
 \multirow{2}{*}{pleasure}& screen size &  &  &  &  &  \\
 & ecological &  &  &  &  & & \tikzmark{bottom} \\ \bottomrule
\end{tabular}%
\begin{tikzpicture}[overlay, remember picture]
  \path (pic cs:top) ++ (0pt,\arraystretch\ht\strutbox+3pt) coordinate (top);
  \path (pic cs:bottom) ++ (0pt,-\arraystretch\dp\strutbox-2pt) coordinate (bottom);
  %\draw[red] (top) -- (bottom);% test run
  \pgfextracty{\tempdima}{\pgfpointdiff{\pgfpointanchor{bottom}{center}}{\pgfpointanchor{top}{center}}}%
  \node[below right, inner sep=0pt] at (top) {\includegraphics[width=2cm, height=\tempdima]{example-image}};
\end{tikzpicture}
\end{table}
\end{document}

演示

相关内容