使用乳胶中的函数以颜色和条件创建并填充表格

使用乳胶中的函数以颜色和条件创建并填充表格

我的演示文稿中有下表,我需要创建一个接受参数的函数,例如

% developer: Boolean if true we will have colored circle in developer row (default true)
% devOps   : Boolean if true we will have colored circle in devOps row (default true)
% business : Boolean if true we will have colored circle in business row (default true)
% watchMethod: Int if 1 we will add all the circle under Computer column, if 2 Mobile/Tablet column else Just listening column (default listening if not passed as argument)
% length   : it is an indecator for colors if 1 red, 2 blue, else green (default green if not passed as an argument)
def makeTargetLectureClassificationTable(developer:Boolean, devOps:Boolean, business:Boolean, watchingMethod:Int, length:Int)={
}

表格代码示例如下


%circle inside table
\newcommand*\redcircled{\tikz\draw[red,fill=red] (0,0) circle (.5ex);}
\newcommand*\bluecircled{\tikz\draw[blue,fill=blue] (0,0) circle (.5ex);}
\newcommand*\greencircled{\tikz\draw[green,fill=green] (0,0) circle (.5ex);}
\begin{table}[t]
    \centering  
    \begin{tabular}{|c |c | c | c|}
        \hline
        \thead{Watching Method \\ / Audience}  & \thead{Computer} & \thead{Mobile/Tablet} &  \thead{Just    listening} \\
        \hline
        \thead{Developer} &   &  \bluecircled & \\
        \hline
        \thead{DevOps}  &  & \bluecircled &  \\
        \hline
        \thead{Business} &  & \bluecircled & \\
        \hline
    \end{tabular}
\end{table}

输出截图如下这里

答案1

我不完全确定我是否理解了你想要什么,但也许下面的代码会有所帮助。我定义了一个\VideoClassification创建视频分类表的宏。此宏接受不同设置的键值对。我对开发等使用了略有不同的语法,因为这更容易实现,并且我使用了对我来说更有意义的名称:

  • 默认情况下,标记被放入三行中,您可以分别使用nodevnodevops和 来“关闭”标记nobus。默认情况下,所有行都有标记
  • 标记出现的列用 指定column=n,其中n123(默认3
  • 标记的颜色由 给出,colour=<col>其中col可以是任何有效颜色(不仅仅是greenbluered)。默认情况下,标记是green

这些以逗号分隔的键可以按任何顺序给出。

使用此语法的命令:

  \VideoClassification
  \VideoClassification[nodev, column=2]
  \VideoClassification[column=1, colour=red]

产生(由于表格环境,会产生大量令人厌烦的额外空间):

在此处输入图片描述

基本思想是使用鍵盤解析 的参数\VideoClassification。键设置颜色和列,然后使用 pgfkey 打印标记,mark该 pgfkey 接受两个参数,分别指定列和行。下面的代码中有更多解释性注释。我相信有更有效的方法来做到这一点!

完整代码如下:

\documentclass{article}
\usepackage{pgfkeys}
\usepackage{makecell}
\usepackage{tikz}

\pgfkeys{/Videos/.is family, /Videos,
  bus/.initial=1,% by default the business row has a marker
  dev/.initial=1,% by default the developer row has a marker
  devops/.initial=1,% by default the developerOps row has a marker
  colour/.initial=green,% the colour of the marker
  column/.initial=3,% the column of the marker
  mark/.code 2 args={% place a marker ONLY if #1=column and \VIDEO{#2}=1
    \ifnum#1=\VIDEO{column}%
      \ifnum1=\VIDEO{#2}%
        \tikz\draw[\VIDEO{colour},fill=\VIDEO{colour}] (0,0) circle (.5ex);%
      \fi%
    \fi%
  },
  nobus/.style={bus=0},% turn off marker for business
  nodev/.style={dev=0},% turn off marker for developer
  nodevops/.style={devops=0},% turn off marker for devoperOps
}
% shortcut to access key values
\newcommand\VIDEO[1]{\pgfkeysvalueof{/Videos/#1}}

% Usage: \VideoClassification[optional key values]
\newcommand\VideoClassification[1][]{%
  \begin{table}[t]
    \pgfkeys{/Videos, #1}% key changes local to group (and macro)
    \centering
    \begin{tabular}{|c|c|c|c|}
        \hline
        \thead{Watching Method \\ / Audience}  & \thead{Computer} & \thead{Mobile/Tablet} &  \thead{Just listening} \\
        \hline
        \thead{Developer}& \pgfkeys{/Videos/mark={1}{dev}}   & \pgfkeys{/Videos/mark={2}{dev}}   & \pgfkeys{/Videos/mark={3}{dev}} \\
        \hline
        \thead{DevOps}   & \pgfkeys{/Videos/mark={1}{devops}}& \pgfkeys{/Videos/mark={2}{devops}}& \pgfkeys{/Videos/mark={3}{devops}} \\
        \hline
        \thead{Business} & \pgfkeys{/Videos/mark={1}{bus}}   & \pgfkeys{/Videos/mark={2}{bus}}   & \pgfkeys{/Videos/mark={3}{bus}} \\
        \hline
    \end{tabular}
  \end{table}
}

\begin{document}

  \VideoClassification

  \VideoClassification[nodev, column=2]

  \VideoClassification[column=1, colour=red]

\end{document}

相关内容