下面的 MWE 创建了一个 3 x 3 的表格。表格上方的文本描述了我希望没有边框的特定单元格。
我相信解决方案在于正确使用\cline
命令并结合\multicolumn{1}{c|}
。
但我无法找出正确的语法来实现期望的结果。
非常感谢您的帮助!
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}
\setlength{\parskip}{\baselineskip}
\title{FACTORbox}
\author{Algebra Teacher Who Struggles with LaTeX Tables}
\date{January 2021}
\begin{document}
\maketitle
In the table below, I only want \textbf{borderlines around the values in black,
} not the red values above and to the left of the black colored values.
Also, I can't figure out a way to \textbf{remove the gray shading from upper left corner cell.}
\def\arraystretch{2}
\begin{tabular}{ |c | c | c | }
% \hline
\cellcolor{gray}& \textcolor{red}{$x$} & \textcolor{red}{$+6$} \\
\hline
\textcolor{red}{$x$} & $x^2$ & $6x$ \\
\hline
\textcolor{red}{$+2$} & $2x$ & $12$ \\
\hline
\end{tabular}
\end{document}
答案1
像这样?
您有很多种可能性,其中之一就是使用\multicolumn
没有边框的命令:
\documentclass{article}
\usepackage[table]{xcolor}
\newcommand\mcc[1]{\multicolumn{1}{c}{#1}} % <---
\setlength{\parskip}{\baselineskip}
\title{FACTORbox}
\author{Algebra Teacher Who Struggles with LaTeX Tables}
\date{January 2021}
\begin{document}
\maketitle
In the table below, I only want \textbf{borderlines around the values in black,
} not the red values above and to the left of the black colored values.
Also, I can't figure out a way to \textbf{remove the gray shading from upper left corner cell.}
\renewcommand\arraystretch{2}
\begin{tabular}{ c | c | c | }
% \hline
\mcc{} & \mcc{\textcolor{red}{$x$}} & \mcc{\textcolor{red}{$+6$}} \\
\cline{2-3}
\textcolor{red}{$x$} & $x^2$ & $6x$ \\
\cline{2-3}
\textcolor{red}{$+2$} & $2x$ & $12$ \\
\cline{2-3}
\end{tabular}
\end{document}
编辑:
另一种可能性是使用˙blkarray
包:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{blkarray}
\begin{document}
$\renewcommand\arraystretch{2}
\begin{blockarray}{r cc}
& \textcolor{red}{x} & \textcolor{red}{+6} \\
\cline{2-3}
\begin{block}{r|c|c|}
\cline{2-3}
\textcolor{red}{x} & x^2 & 6x \\
\cline{2-3}
\textcolor{red}{+2} & 2x & 12 \\
\cline{2-3}
\end{block}
\end{blockarray}
$
\end{document}
结果和以前一样。
答案2
带包裹nicematrix
。
\documentclass{article}
\usepackage{nicematrix}
\usepackage{xcolor}
\begin{document}
\renewcommand\arraystretch{1.8}
$\begin{NiceMatrix}%
[
hvlines,
columns-width=auto,
first-row, code-for-first-row = \color{red},
first-col, code-for-first-col = \color{red}
]
& x & +6 \\
x & x^2 & 6x \\
+2 & 2x & 12
\end{NiceMatrix}$
\end{document}
您需要多次编译(因为nicematrix
使用 PGF/Tikz 节点)。