如何修复最后一个单元格过满

如何修复最后一个单元格过满

你能帮忙修复下一个代码吗?我想修复这个向量的最后一个单元格。如你所见,最后一个单元格颜色过满。

在此处输入图片描述

    \documentclass{article}
    \usepackage[utf8]{inputenc}
    \usepackage{amsfonts}
    \usepackage{amsmath}
    \usepackage[square,sort,comma,numbers]{natbib}
    \usepackage{float}
    \usepackage{graphicx}
    
    \usepackage{tikz}
    \usepackage{collcell}
    
     %The min, mid aund max values
    \newcommand*{\MinNumber}{-1.0}%
    \newcommand*{\MidNumber}{0} %
    \newcommand*{\MaxNumber}{1.0}%
    
    %Apply the gradient macro
    \newcommand{\ApplyGradient}[1]{
            \ifdim #1 pt > \MidNumber pt
                \pgfmathsetmacro{\PercentColor}{max(min(100.0*(#1 - \MidNumber)/(\MaxNumber-\MidNumber),100.0),0.00)}
                \colorbox{blue!\PercentColor!white}{}
            \else
                \pgfmathsetmacro{\PercentColor}{max(min(100.0*(\MidNumber - #1)/(\MidNumber-\MinNumber),100.0),0.00)}
                \colorbox{red!\PercentColor!white}{}
            \fi
    }
    
    
    \newcolumntype{R}{|>{\collectcell\ApplyGradient}p{1mm}<{\endcollectcell}}
    %\newcolumntype{R}{>{\collectcell\ApplyGradient}c<{\endcollectcell}}
    \renewcommand{\arraystretch}{0}
    \setlength{\fboxsep}{1mm} % box size
    \setlength{\tabcolsep}{0pt}
    
    
    \begin{document}
    
    \begin{table}
    \begin{tabular}{RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR|} \hline
    0.0 & 0.0 & 1.0 & 0.0 & 0.33 & 0.33 & 0.0 & 0.33 & 1.0 & 0.0 & 0.0 & 0.0 & 0.33 & 0.33 & 0.0 & 0.33 & 0.0 & 0.0 & 1.0 & 0.0 & 0.67 & 0.0 & 0.0 & 0.33 & 0.0 & 0.0 & 0.0 & 0.0 & 0.33 & 0.67 & 1.0 & 0.3 \\ \hline
    \end{tabular}
    \end{table}
\end{document}

答案1

我没有使用带框的彩色盒子,而是插入了 1 x 2mm 的规则。

表格定义为零垂直填充、零列宽,且\tabcolsep等于框的宽度,因此规则将适合。单元格的高度由框的高度决定。

w8

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage[square,sort,comma,numbers]{natbib}
\usepackage{float}
\usepackage{graphicx}

\usepackage{tikz}
\usepackage{collcell}

%The min, mid aund max values
\newcommand*{\MinNumber}{-1.0}%
\newcommand*{\MidNumber}{0.0} %
\newcommand*{\MaxNumber}{1.0}%

\newlength{\Bwidth} % width ahd height of the boxes
\newlength{\Bheight}    
\setlength{\Bwidth}{1mm}
\setlength{\Bheight}{2mm}   

%Apply the gradient macro
\newcommand{\ApplyGradient}[1]{% using rules
    \ifdim #1 pt > \MidNumber pt
    \pgfmathsetmacro{\PercentColor}{max(min(100.0*(#1 - \MidNumber)/(\MaxNumber-\MidNumber),100.0),0.00)}
        \color{blue!\PercentColor!white}\rule{\Bwidth}{\Bheight} 
    \else
    \pgfmathsetmacro{\PercentColor}{max(min(100.0*(\MidNumber - #1)/(\MidNumber-\MinNumber),100.0),0.00)}
        \color{red!\PercentColor!white}\rule{\Bwidth}{\Bheight}
    \fi
}

\newcolumntype{T}{>{\collectcell\ApplyGradient}wc{0mm}<{\endcollectcell}} % zero width 

\begin{document}    

\setlength{\tabcolsep}{\Bwidth} % equal to box width
\renewcommand{\arraystretch}{0.0} % zero vertical padding

\setlength\arrayrulewidth{0.3pt} 
\addtolength{\Bwidth}{-1.6\arrayrulewidth}

\noindent
\begin{tabular}{|*{32}{@{\hspace*{-\arrayrulewidth}}T|@{\hspace*{-0.6\arrayrulewidth}}}|} % adjust hspace to center
    \hline
    0.0 & 0.0 & 1.0 & 0.0 & 0.33 & 0.33 & 0.0 & 0.33 & 1.0 & 0.0 & 0.0 & 0.0 & 0.33 & 0.33 & 0.0 & 0.33 & 0.0 & 0.0 & 1.0 & 0.0 & 0.67 & 0.0 & 0.0 & 0.33 & 0.0 & 0.0 & 0.0 & 0.0 & 0.33 & 0.67 & 1.0 & 0.3 \\ 
    \hline
\end{tabular}

\end{document}

\fboxsep 设置盒子周围的分隔,因此它是一个正方形。

在您的代码中尝试

\newcolumntype{R}{|>{\collectcell\ApplyGradient}wc{0mm}<{\endcollectcell}}

\setlength{\tabcolsep}{1mm}看看发生了什么事。

X

然后使用 \colorbox{blue!\PercentColor!white}{}\hspace*{3.4pt}

要得到

是

答案2

如果我理解你的问题正确的话,你实际上正在寻找显示的图像。它可以简单地绘制为节点链,其颜色在循环tikzpicture中定义:\foreach

\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{chains,
                positioning}

\begin{document}
    \begin{tikzpicture}[
node distance = 0pt,
  start chain = going right,
     N/.style = {draw, fill=blue!#1!white, minimum width=2mm, minimum height=7mm,
                 inner sep=0pt, outer sep=0pt,
                 node contents={}, on chain}
                        ]
\foreach \x [evaluate=\x as \i using 100*\x] in {
    0.0,  0.0, 1.0, 0.0, 0.33, 0.33, 0.0, 0.33, 1.0,  0.0, 0.0, 0.0, 0.33, 0.33, 0.0, 0.33, 0.0, 0.0, 1.0, 0.0,  0.67, 0.0, 0.0,  0.33, 0.0, 0.0, 0.0, 0.0,  0.33, 0.67, 1.0,  0.3}
\node[N=\i];
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容