有没有办法制作用于构建这种表格的宏?

有没有办法制作用于构建这种表格的宏?

有没有办法制作宏来制作这样的表格,其中颜色根据不同的单元格而变化,即使前几个单元格是多行单元格。我需要制作 16 个这样的表格,其中列数、单元格数等都不同,这似乎是一项巨大的工作,可以稍微自动化一下。我真的不知道该怎么做。

Table made using the severely impractical method shown below below

生成表格的代码如下:

\documentclass{article}
\usepackage[utf8]{inputenc}

%===================================

\usepackage{float}
\usepackage{tabu}
\usepackage[dvipsnames,table,longtable,x11names]{xcolor}                % Definer farver med \definecolor. Se mere: 
\usepackage{multirow} % Fletning af raekker og kolonner (\multicolumn og 
\multirow) 
\usepackage{hhline} % more options for lines between rows and columns
\newcolumntype{P}[1]{>{\centering\arraybackslash}m{#1}} % New column type P with centering

\usepackage{geometry}
 \geometry{ a4paper, total={180mm,257mm}, left=15mm, top=60mm, bottom=0mm, textheight=180mm, footskip=10mm, headheight=52pt, }

% ====================== HARDWARE ARKITEKTUR ====================================
\begin{document}
\section{HARDWARE ARKITEKTUR} 


\begin{table}[H]
\begin{tabular}[c]{|p{2cm}|p{7cm}|P{3.5cm}|P{3.5cm}|}
\hhline{>{\arrayrulecolor{black}}|-|-|--}
    \rowcolor{NavyBlue}\textcolor{White}{\textbf{Blok-navn}} & \textcolor{White}{\textbf{Funktionsbeskrivelse}}  & \textcolor{White}{\textbf{Signaler [Porte]} }& \textcolor{White}{\textbf{Kommentar}} \\


\hhline{>{\arrayrulecolor{black}}|-|-|--}
    \cellcolor{NavyBlue!35}  & \cellcolor{NavyBlue!35} & \cellcolor{NavyBlue!35} Force[userChoice]  & \cellcolor{NavyBlue!35} Brugerens input\\
\hhline{>{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!35}}->{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!35}}->{\arrayrulecolor{black}}|--}
    \cellcolor{NavyBlue!35}  & \cellcolor{NavyBlue!35} & \cellcolor{NavyBlue!15} SVDC [vcc]         & \cellcolor{NavyBlue!15} Spændingsforsyning\\
\hhline{>{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!35}}->{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!35}}->{\arrayrulecolor{black}}|--}
    \cellcolor{NavyBlue!35}  & \cellcolor{NavyBlue!35} & \cellcolor{NavyBlue!35} Light [display]        & \cellcolor{NavyBlue!35} Viser brugerens muligheder/valg\\
\hhline{>{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!35}}->{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!35}}->{\arrayrulecolor{black}}|--}
    \cellcolor{NavyBlue!35}  & \cellcolor{NavyBlue!35} & \cellcolor{NavyBlue!15}I2C [gameStatus]    & \cellcolor{NavyBlue!15}Kommunikation mellem CPU'er\\
\hhline{>{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!35}}->{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!35}}->{\arrayrulecolor{black}}|--}
    \cellcolor{NavyBlue!35} \multirow[t]{-7.5}{\hsize}{Score system} & 
    \cellcolor{NavyBlue!35} \multirow[t]{-7.5}{\hsize}{Indeholder brugergrænsefladen som blandt andet er et tegn for en and i en båd med et kosteskaft og en trillebør} & \cellcolor{NavyBlue!35} WIFI [wifi] & \cellcolor{NavyBlue!35} Forbindelse til Database\\


\hhline{>{\arrayrulecolor{black}}|-|-|--}


    \cellcolor{NavyBlue!15} & \cellcolor{NavyBlue!15}  &  \cellcolor{NavyBlue!15}5VDC [vcc] & \cellcolor{NavyBlue!15}Spændingsforsyning\\
\hhline{>{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|--} 
    \cellcolor{NavyBlue!15}  & \cellcolor{NavyBlue!15} & \cellcolor{NavyBlue!35} 12VDC [powerRail] & \cellcolor{NavyBlue!35} Spændingsforsyning\\
\hhline{>{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|--}
    \cellcolor{NavyBlue!15}  & \cellcolor{NavyBlue!15} & \cellcolor{NavyBlue!15} IR [dryerSensor] & \cellcolor{NavyBlue!15} IR modtager\\
\hhline{>{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|--}
    \cellcolor{NavyBlue!15}  & \cellcolor{NavyBlue!15} & \cellcolor{NavyBlue!35} IR [cupSensor] & \cellcolor{NavyBlue!35} IR modtager\\
\hhline{>{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|--}
    \cellcolor{NavyBlue!15}  & \cellcolor{NavyBlue!15} & \cellcolor{NavyBlue!15} IR [dryerLED] & \cellcolor{NavyBlue!15} IR modtager\\
\hhline{>{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|--}
    \cellcolor{NavyBlue!15}  & \cellcolor{NavyBlue!15} & \cellcolor{NavyBlue!35} IR [cupLED] & \cellcolor{NavyBlue!35}IR modtager\\
\hhline{>{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|--}
    \cellcolor{NavyBlue!15}  & \cellcolor{NavyBlue!15} & \cellcolor{NavyBlue!15} Light [target] & \cellcolor{NavyBlue!15}Visuelt lys fra kopperne\\
\hhline{>{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|--}
    \cellcolor{NavyBlue!15}  & \cellcolor{NavyBlue!15} & \cellcolor{NavyBlue!35} Airflow [airflow] & \cellcolor{NavyBlue!35}Luft til at tørre bolden\\
\hhline{>{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|--}
    \cellcolor{NavyBlue!15} \multirow[t]{-10.5}{\hsize}{Cup Zone} & 
    \cellcolor{NavyBlue!15} \multirow[t]{-10.5}{\hsize}{ Sørger for detektering af kopper og bolde i kopperne, og aktivering af motoren i Ball Dryer. Desuden styres belysning under kopperne også her.} & \cellcolor{NavyBlue!15} I2C [gameStatus] & \cellcolor{NavyBlue!15}Kommunikation mellem blokkene\\


\hhline{>{\arrayrulecolor{black}}|-|-|--}


    \cellcolor{NavyBlue!35}  & \cellcolor{NavyBlue!35} & \cellcolor{NavyBlue!35}5VDC [vcc] & \cellcolor{NavyBlue!35}Spændingsforsyning\\
\hhline{>{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!35}}->{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!35}}->{\arrayrulecolor{black}}|--}
    \cellcolor{NavyBlue!35}  & \cellcolor{NavyBlue!35} & \cellcolor{NavyBlue!15}12VDC [powerRail] & \cellcolor{NavyBlue!15}Spændingsforsyning\\
\hhline{>{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!35}}->{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|--} 
    \cellcolor{NavyBlue!35}  & \cellcolor{NavyBlue!35} & \cellcolor{NavyBlue!35} Sound [audio] & \cellcolor{NavyBlue!35} Musik der analyseres\\
\hhline{>{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!35}}->{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!35}}->{\arrayrulecolor{black}}|--}
    \cellcolor{NavyBlue!35}  & \cellcolor{NavyBlue!35} & \cellcolor{NavyBlue!15} Light [matrixLight] & \cellcolor{NavyBlue!15} Bordplade lysmønster\\
\hhline{>{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!35}}->{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|--}
    \cellcolor{NavyBlue!35}  & \cellcolor{NavyBlue!35} & \cellcolor{NavyBlue!35} Light [bgLight] &\cellcolor{NavyBlue!35} Bordplade baggrundsbelysning\\
\hhline{>{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!35}}->{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!35}}->{\arrayrulecolor{black}}|--}
    \cellcolor{NavyBlue!35} \multirow[t]{-7.5}{\hsize}{Light Panel} & 
    \cellcolor{NavyBlue!35} \multirow[t]{-7.5}{\hsize}{Her styres den grafiske belysning på bordet. Belysningen består af en matrix og baggrundsbelysning.} & \cellcolor{NavyBlue!15} I2C [gameStatus] & \cellcolor{NavyBlue!15} Kommunikation bellem blokkene\\


\hhline{>{\arrayrulecolor{black}}|-|-|--}


    \cellcolor{NavyBlue!15}  & \cellcolor{NavyBlue!15} & \cellcolor{NavyBlue!35} 5VDC [vcc] & \cellcolor{NavyBlue!35} Spændingsforsyning\\
\hhline{>{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|>{\arrayrulecolor{NavyBlue!15}}->{\arrayrulecolor{black}}|--}
    \cellcolor{NavyBlue!15} \multirow[t]{-2}{\hsize}{Power Supply} & 
    \cellcolor{NavyBlue!15} \multirow[t]{-2}{\hsize}{Forsyner systemet med strøm} & \cellcolor{NavyBlue!15} 12VDC [powerRail] & \cellcolor{NavyBlue!15} Spændingsforsyning\\


\hhline{>{\arrayrulecolor{black}}|-|-|--}
\end{tabular} 

\caption{Blokbeskrivelse APP}
\label{Blokbeskrivelse APP}
\end{table}

\end{document}

最后,在这个 URL 上有一个可以工作的示例:ShareLatex 工作示例

答案1

使用新的表格数组包。包允许格式和内容分离,这使得代码更加清晰。

\documentclass{article}

\usepackage{float}
\usepackage[dvipsnames,table,longtable,x11names]{xcolor}
\usepackage{tabularray}

\usepackage{geometry}
\geometry{ a4paper, total={180mm,257mm}, left=15mm, top=60mm, bottom=0mm, textheight=180mm, footskip=10mm, headheight=52pt, }

\begin{document}

\NewColumnType{P}[1]{Q[c,wd=#1]}

\begin{table}[H]
    \begin{tblr}{
            colspec = {|p{2cm}|p{7cm}|P{3.5cm}|P{3.5cm}|},
            row{even} = {NavyBlue!35}, row{odd} = {NavyBlue!15},
            row{1} = {bg=NavyBlue, fg=white, font=\bfseries},
            hlines,
        }
        Blok-navn & Funktionsbeskrivelse & Signaler [Porte] & Kommentar \\
        \SetCell[r=5]{h, NavyBlue!35} Score system & \SetCell[r=5]{h, NavyBlue!35} Indeholder brugergrænsefladen som blandt andet er et tegn for en and i en båd med et kosteskaft og en trillebør & Force[userChoice] & Brugerens input \\
        && SVDC [vcc] & Spændingsforsyning \\
        && Light [display] & Viser brugerens muligheder/valg \\
        && I2C [gameStatus] & Kommunikation mellem CPU'er \\
        && WIFI [wifi] & Forbindelse til Database \\
        % next block
        \SetCell[r=9]{h, NavyBlue!15} Cup Zone & \SetCell[r=9]{h, NavyBlue!15} Sørger for detektering af kopper og bolde i kopperne, og aktivering af motoren i Ball Dryer. Desuden styres belysning under kopperne også her. & 5VDC [vcc] & Spændingsforsyning \\
        && 12VDC [powerRail] & Spændingsforsyning \\
        && IR [dryerSensor] & IR modtager \\
        && IR [cupSensor] & IR modtager \\
        && IR [dryerLED] & IR modtager \\
        && IR [cupLED] & IR modtager \\
        && Light [target] & Visuelt lys fra kopperne \\
        && Airflow [airflow] & Luft til at tørre bolden \\
        && I2C [gameStatus] & Kommunikation mellem blokkene \\
        % next block
        \SetCell[r=6]{h, NavyBlue!35} Light Panel & \SetCell[r=6]{h, NavyBlue!35} Her styres den grafiske belysning på bordet. Belysningen består af en matrix og baggrundsbelysning. & 5VDC [vcc] & pændingsforsyning \\
        && 12VDC [powerRail] & Spændingsforsyning \\
        && Sound [audio] & Musik der analyseres \\
        && Light [matrixLight] & Bordplade lysmønster \\
        && Light [bgLight] & Bordplade baggrundsbelysning \\
        && I2C [gameStatus] & Kommunikation bellem blokkene \\
        % next block
        \SetCell[r=2]{h, NavyBlue!15} Power Supply & \SetCell[r=2]{h, NavyBlue!15} Forsyner systemet med strøm & 5VDC [vcc] & Spændingsforsyning \\
        && 12VDC [powerRail] & Spændingsforsyning \\
    \end{tblr}
    \caption{Blokbeskrivelse APP}
    \label{Blokbeskrivelse APP}
\end{table}
    
\end{document}

产生输出

output

您可以在参数中指定奇数行和偶数行的颜色格式tblr,还可以为标题添加不同的格式。使用该命令,\SetCell您可以创建跨多行的单元格(类似于多行)。由于您的块大小不总是相同,因此您必须具体指定行数和颜色。

我不太理解你的 hline 命令,所以我刚刚添加了一个选项,hlinestblr需要的地方创建黑色 hline。你也可以自定义它,请参阅文档更多信息。

既然您说您有许多这样的表,那么根据这种风格定义一个新环境是有意义的。

\NewTblrEnviron{mytblr}
\SetTblrInner[mytblr]{
    row{even} = {NavyBlue!35}, row{odd} = {NavyBlue!15},
    row{1} = {bg=NavyBlue, fg=white, font=\bfseries},
    hlines,
}

\begin{mytblr}{|p{2cm}|p{7cm}|P{3.5cm}|P{3.5cm}|}
   % content
\end{mytblr}

相关内容