Beamer:為表格的每隔一列著色

Beamer:為表格的每隔一列著色

我試圖在我的一個投影機框架中包含一張桌子。我希望該表的每隔一列都有灰色背景。在我的 LaTeX 文件中,以下程式碼有效:

\definecolor{Gray}{gray}{0.9}
\newcolumntype{g}{>{\columncolor{Gray}}c}
\begin{table}[ht]
            \centering
            \begin{tabular}{c|g|c|g|c|g}
                5.35 & 0.35 & 5.61 & -0.39 & 6.84 & -0.16 \\
                5.45 & 0.45 & 5.52 & -0.48 & 6.84 & -0.16 \\
                5.45 & 0.45 & 5.51 & -0.49 & 6.96 & -0.04 \\
                5.45 & 0.45 & 5.63 & -0.37 & 6.96 & -0.04 
            \end{tabular}
            \caption{my caption.}
            \label{myTable}
        \end{table}

然而,這在投影機中不起作用。我正在使用該\usepackage{xcolor}包。現在,我發現這個部落格我應該改變我的文檔類別來\documentclass[xcolor=table]{beamer}使用\usepackage[table]{xcolor},然後一切都應該工作。這裡的問題是我使用的是自訂模板而不是投影機模板。無論如何,我無法理解這個問題,它不應該那麼複雜,不是嗎?

不幸的是,彈出的錯誤訊息根本沒有幫助。它只是說:Undefined control sequence \end{frame}

答案1

beamer加載xcolor包。如果你想新增一些選項xcolor,你應該在文件類別選項之間寫這個選項:

\documentclass[table]{beamer} % <--- option "table"  is passed to "xcolor" pacage
\definecolor{Gray}{gray}{0.9}
\newcolumntype{g}{>{\columncolor{Gray}}c}

\begin{document}
\begin{frame}
\frametitle{My colored table}
\begin{table}% table in beamer isn't float ...
    \centering
    \begin{tabular}{c|g|c|g|c|g}
        5.35 & 0.35 & 5.61 & -0.39 & 6.84 & -0.16 \\
        5.45 & 0.45 & 5.52 & -0.48 & 6.84 & -0.16 \\
        5.45 & 0.45 & 5.51 & -0.49 & 6.96 & -0.04 \\
        5.45 & 0.45 & 5.63 & -0.37 & 6.96 & -0.04
    \end{tabular}
    \caption{my caption.}
    \label{myTable}
\end{table}
\end{frame}
\end{document}

它給出了期望的結果:

在此輸入影像描述

答案2

因為tikzpicture在班級中適應得很好beamer(它們是同一作者Till Tantao寫的),所以可以將表格製作為tikzpicture.無需加載選項[table]

在此輸入影像描述

\documentclass{beamer}
\usepackage{tikz}
% I mimick the way of putting a row in a table
\newcommand{\putrow}[7]{
\path (0,#1) node{#2}
++(0:1) node{#3} ++(0:1) node{#4}
++(0:1) node{#5} ++(0:1) node{#6} ++(0:1) node{#7};
}

\begin{document}
\begin{frame}
\frametitle{Colored table}
\centering
\begin{tikzpicture}[xscale=1.2,yscale=.5]
\begin{scope}[shift={(-.5,.5)}]
\foreach \i in {1,3,5} \fill[blue!20] (\i,0) rectangle +(1,-4);
\foreach \i in {1,...,6} \draw[blue] (\i,0)--+(-90:4);
\end{scope}
\putrow{0}{5.35}{0.35}{5.61}{-0.39}{6.84}{-0.16}
\putrow{-1}{5.45}{0.45}{5.52}{-0.48}{6.84}{-0.16}
\putrow{-2}{5.45}{0.45}{5.51}{-0.49}{6.96}{-0.04}
\putrow{-3}{5.45}{0.45}{5.63}{-0.37}{6.96}{-0.04}

\path (current bounding box.south) node[below=2mm,blue]
{My colored table as a tikzpicture};
\end{tikzpicture}
\end{frame}
\end{document}

相關內容