无法使 \cellcolor 在表格上工作

无法使 \cellcolor 在表格上工作

我已经使用下表生成器站点来创建下表,但感觉它比需要的稍微复杂一些,而且我无法让颜色单元格工作。https://www.tablesgenerator.com/

代码有什么问题?我是否拥有所有必需的软件包?

我还想了解 \multicolumn 到底是怎么回事,这有必要吗?

\documentclass{beamer}
\usepackage{xcolor}

\usepackage{color}
\usepackage{booktabs}
\usepackage{latexsym}

\usepackage{environ}
\usepackage{varwidth}
%\usepackage[sorting=none]{biblatex}
\usepackage{media9}
\usetheme[progressbar=frametitle]{metropolis}
\setbeamertemplate{frame numbering}[fraction]
\useoutertheme{metropolis}
\useinnertheme{metropolis}
%\usefonttheme{metropolis}
\usepackage{graphicx}
\usepackage{mathtools}
\usecolortheme{spruce}
\setbeamercolor{background canvas}{bg=white}
\usepackage{kpfonts}
\usepackage{amsmath}
\definecolor{mygreen}{rgb}{.125,.5,.25}
\title{Investigating Consonance and Dissonance}
\subtitle{\small By: Max Budnick}
\usepackage{cases}
\usepackage{multimedia}
%\institute{Perimeter College, Dunwoody, G.A.}
\date{}

\usepackage[export]{adjustbox}
\graphicspath{ {./images/} }
\usefonttheme[onlymath]{serif}
\usepackage[Symbol]{upgreek}
\usepackage{lipsum}
\usepackage{amsmath}
\usepackage{etoolbox}
\newcommand{\zerodisplayskips}{%
  \setlength{\abovedisplayskip}{0pt}%
  \setlength{\belowdisplayskip}{0pt}%
  \setlength{\abovedisplayshortskip}{0pt}%
  \setlength{\belowdisplayshortskip}{0pt}}
\appto{\normalsize}{\zerodisplayskips}
\appto{\small}{\zerodisplayskips}
\appto{\footnotesize}{\zerodisplayskips}
\usepackage[sorting=none]{biblatex}
\AtBeginBibliography{\footnotesize}
\appto\bibsetup{\scriptsize}
%\setlength\bibitemsep{\baselineskip}
\addbibresource{Presentation.bib}
\usepackage{comment}

\begin{document}

\begin{frame}[t]{Equal Temperament} 
\begin{figure}
\begin{table}[]
\scalebox{0.7}{
\begin{tabular}{llll}
\cline{1-4}
             
                     
\multicolumn{1}{|l|}{\textbf{\textit{Interval Name}}}     &     \multicolumn{1}{|l|}{\textbf{\textit{Notes}}}        & \multicolumn{1}{|l|}{\textbf{\textit{Consonant/Dissonant}}} & \multicolumn{1}{c|}{\textbf{\textit{Expressed as a Function}}} \\ \hline
\multicolumn{1}{|l|}{Perfect Unison} & \multicolumn{1}{l|}{C4 | C4} & \multicolumn{1}{l|}{Consonant}     & \multicolumn{1}{l|}{$f(x)= \sin{261.63\uppi x} +\sin{261.63\uppi x}$}    \\ \hline
\multicolumn{1}{|l|}{Minor 2nd}      & \multicolumn{1}{l|}{C4 | D$\flat$4}  & \multicolumn{1}{l|}{Dissonant}      & \multicolumn{1}{l|}{261.63}          \\ \hline
\multicolumn{1}{|l|}{Major 2nd}      & \multicolumn{1}{l|}{C4 | D4}  & \multicolumn{1}{l|}{Dissonant}      & \multicolumn{1}{l|}{277.18}          \\ \hline
\multicolumn{1}{|l|}{Minor 3rd}      & \multicolumn{1}{l|}{C4 | E$\flat$4}  & \multicolumn{1}{l|}{Consonant}      & \multicolumn{1}{l|}{293.66}          \\ \hline
\multicolumn{1}{|l|}{Major 3rd}      & \multicolumn{1}{l|}{C4 | E4}  & \multicolumn{1}{l|}{Consonant}      & \multicolumn{1}{l|}{311.13}          \\ \hline
\multicolumn{1}{|l|}{Perfect 4th}      & \multicolumn{1}{l|}{C4 | F4}  & \multicolumn{1}{l|}{Consonant}      & \multicolumn{1}{l|}{329.63}          \\ \hline
%\multicolumn{1}{|l|}{Augmented 4th}      & \multicolumn{1}{l|}{f\#}  & \multicolumn{1}{l|}{$2^{1/2}$}      & \multicolumn{1}{l|}{349.23}          \\ \hline
\multicolumn{1}{|l|}{Tritone}      & \multicolumn{1}{l|}{\cellcolor{blue}C4 | F$\sharp$4}  & \multicolumn{1}{l|}{Dissonant}      & \multicolumn{1}{l|}{369.99}          \\ \hline
\multicolumn{1}{|l|}{Perfect 5th}      & \multicolumn{1}{l|}{C4 | G4}  & \multicolumn{1}{l|}{Consonant}      & \multicolumn{1}{l|}{391.99}          \\ \hline
\multicolumn{1}{|l|}{Minor 6th}      & \multicolumn{1}{l|}{C4|A$\flat$4}  & \multicolumn{1}{l|}{$2^{2/3}$}      & \multicolumn{1}{l|}{415.30}          \\ \hline

\multicolumn{1}{|l|}{Major 6th}      & \multicolumn{1}{l|}{C4|A4}  & \multicolumn{1}{l|}{$2^{3/4}$}      & \multicolumn{1}{l|}{440}          \\ \hline
\multicolumn{1}{|l|}{Minor 7th}      & \multicolumn{1}{l|}{C4|B$\flat$4}  & \multicolumn{1}{l|}{$2^{5/6}$}      & \multicolumn{1}{l|}{466.16}          \\ \hline
\multicolumn{1}{|l|}{Major 7th}      & \multicolumn{1}{l|}{C4|B4}  & \multicolumn{1}{l|}{$2^{11/12}$}      & \multicolumn{1}{l|}{494}          \\ \hline
\multicolumn{1}{|l|}{Perfect Octave}      & \multicolumn{1}{l|}{C4|C5}  & \multicolumn{1}{l|}{$2$}      & \multicolumn{1}{l|}{523.25}          \\ \hline
                           &                        &                            &                               
\end{tabular}}
\end{table}



\end{figure}
\end{frame}

\end{document}

答案1

除了表格中一个单元格的颜色问题之外,您的文档示例还存在许多其他问题:

  • 您应该意识到,加载许多包,beamerxcolor,,,...amsmathgraphicxhyperref
  • 这些包的选项可以写成 `beamer 的选项。在你的情况下,你应该写
\documentclass[xcolor={table}]{beamer}

而不是\documentclass{beamer},您的问题就会消失。

  • 有些包需要加载两次。删除所有重复的包!
  • 乍一看,使用表格生成器似乎是一种方便的解决方案,但结果代码却非常混乱
  • 缩放表格,使其适合框架不是一个好主意,最好重新设计一下表格。尝试在列标题中写入长文本,并在两行中​​选择较小的字体(例如\scriptsize)。
  • 对于最后一列,我将使用S“siunitx 包”中定义的列类型。
  • 使用的标准beamercm(计算机现代)字体没有斜体粗体版本。对于它们,您应该加载lmodernnewtxtext, newtxmath或...

除了上述内容之外,您还可以考虑使用tabularray带有库的包varwidth作为siunitx您的表。使用它,您的 MWE 可以是:

\documentclass[xcolor={table}]{beamer}
\usepackage{mathtools}
\usepackage{newtxtext, newtxmath}
%\usepackage{lmodern}       % <--- alternative to standard cm fonts
\usepackage{tabularray}
\UseTblrLibrary{varwidth, siunitx}

\begin{document}
\begin{frame}[t]
\frametitle{Equal Temperament} 
\begin{table}
    \centering
\caption{My table}
\label{tab:tblr}
    \scriptsize
\begin{tblr}{hlines, vlines,
             colspec={*{3}{ Q[c,m] }
                            Q[c,m, si={table-format=3.2}] },
             row{1} ={font=\itshape\bfseries\linespread{0.84}\selectfont},
             rowsep ={1pt},
             measure=vbox
                 }
{Interval\\ Name}
            & Notes & {Consonant/\\ Dissonant}
                                & {{{Expressed\\ as a Function}}}   
                                                            \\
Perfect Unison
            & C4 -- C4          & Consonant & {{{$\begin{multlined}
                                                f(x)= \sin 261.63\pi x \\[-1ex]
                                                        {} + \sin 261.63\pi x
                                               \end{multlined}$}}}  
                                                            \\
Minor 2nd   & C4 -- D$\flat$4   & Dissonant     & 261.63    \\
Minor 2nd   & C4 -- D$\flat$4   & Dissonant     & 261.63    \\
Major 2nd   & C4 -- D4          & Dissonant     & 277.18    \\
Minor 3rd   & C4 -- E$\flat$4   & Consonant     & 293.66    \\
Major 3rd   & C4 -- E4          & Consonant     & 311.13    \\
Perfect 4th & C4 -- F4          & Consonant      & 329.63   \\
Tritone     & \SetCell[c=1]{bg=blue!30} C4 -- F$\sharp$4 
                                & Dissonant     & 369.99    \\
Perfect 5th & C4 -- G4          & Consonant     & 391.99    \\
Minor 6th   & C4 -- A$\flat$4   & $2^{2/3}$     & 415.30    \\
Major 6th   & C4 -- A4          & $2^{3/4}$     & 440       \\
Minor 7th   & C4 -- B$\flat$4   & $2^{5/6}$     & 466.16    \\
Major 7th   & C4 -- B4          & $2^{11/12}$   & 494       \\
Perfect Octave
            & C4 -- C5          & $2$           & 523.25    \\
    \end{tblr}
\end{table}
\end{frame}
\end{document}

在此处输入图片描述

答案2

我需要包含一个缺少的包裹。

\usepackage{colortbl}

这修复了我的错误。

相关内容