TiKZ 和多行字间距颜色填充

TiKZ 和多行字间距颜色填充

我正在尝试在 TikZ 中制作一个我想要的图形: 如图

我已经能够在 TikZ 中想出一些接近的东西,但我真的很想得到上面的图而不是我的解决方案。

\usepackage[usenames,dvipsnames]{color}
\usepackage{tikz}
\usepackage{booktabs}
\usetikzlibrary{shapes,arrows}

\tikzstyle{block3} = [rectangle, draw, fill=green!20,
text width=15em, text centered, minimum height=4em]
\tikzstyle{block4} = [rectangle, draw, fill=purple!20,
text width=12em, text centered, minimum height=4em]
\tikzstyle{block5} = [rectangle, draw, fill=red!20,
text width=9em, text centered, minimum height=4em]

\begin{document}

\begin{figure}[h!]
\begin{center}
\begin{tikzpicture}
% Place nodes
\node [block3,align=center,xshift=-30em] (as) {ASSETS \hline \\{\color{cyan}Cash + Accounts Receivable + Prepaid Insurance + Equipment + Truck}};
\node [draw=white] at (-7.5,0) (equal) {=};
\node [block4,align=center,right of=as,xshift=13em] (li) {LIABILITIES \hline\\{\color{purple}Bank Loan + Accounts Payable + Unearned Revenue}};
\node [draw=white] at (-2.3,0) (plus) {+};
\node [block5,align=center,right of=li,xshift=11em] (eq) {EQUITY \hline     \\{\color{red}Share Capital + Retained Earnings}};
% Draw edges
\end{tikzpicture}
\end{center}
\end{figure}

\end{document}

资产

答案1

你不需要tikz这个。简单tabular就可以了。

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{array,booktabs}
\newcommand{\head}[1]{%       %% code stolen from egreg
  \begin{tabular}{@{}c@{}}
  \strut#1\strut
  \end{tabular}%
}
\newcolumntype{C}{>{\columncolor{cyan!20}\color{cyan}}c}
\newcolumntype{B}{>{\columncolor{blue!10}\color{blue}}c}
\newcolumntype{R}{>{\columncolor{red!10}\color{red}}c}
\begin{document}
\noindent
{\footnotesize
  \begin{tabular}{CcBcR}
   ASSETS & $=$ & LIABILITIES & $+$ &  EQUITY \\\noalign{\vspace{-2pt}} \cmidrule[1pt]{1-1}\cmidrule[1pt]{3-3}\cmidrule[1pt]{5-5}\noalign{\vspace{-3pt}}
Cash $+$ \head{Accounts \\ Receivable} $+$ \head{Prepaid \\Insurance} $+$ Equipment $+$ Truck
 & $=$ &
 \head{Bank \\ Loan} $+$ \head{Accounts \\ Payable} $+$ \head{Unearned \\ Revenue}
 & $+$ &
 \head{Share \\ Capital} $+$ \head{Retained \\ Earnings}
  \end{tabular}
}
\end{document}

在此处输入图片描述

如果要将符号对齐到顶部:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{array,booktabs}
\newcommand{\head}[1]{%       %% code stolen from egreg
  \begin{tabular}{@{}c@{}}
  \strut#1\strut
  \end{tabular}%
}
\newcolumntype{C}{>{\columncolor{cyan!20}\color{cyan}}c}
\newcolumntype{B}{>{\columncolor{blue!10}\color{blue}}c}
\newcolumntype{R}{>{\columncolor{red!10}\color{red}}c}
\begin{document}
\noindent
{\footnotesize
  \begin{tabular}{CcBcR}
   ASSETS & $=$ & LIABILITIES & $+$ &  EQUITY \\\noalign{\vspace{-2pt}} \cmidrule[1pt]{1-1}\cmidrule[1pt]{3-3}\cmidrule[1pt]{5-5}\noalign{\vspace{-3pt}}
\head{Cash \\} \head{$+$ \\} \head{Accounts \\ Receivable} \head{$+$ \\} \head{Prepaid \\Insurance} \head{$+$ \\} \head{Equipment\\} \head{$+$ \\} \head{Truck\\}
 & \head{$=$\\} &
 \head{Bank \\ Loan} \head{$+$ \\} \head{Accounts \\ Payable} \head{$+$ \\} \head{Unearned \\ Revenue}
 & \head{$+$ \\} &
 \head{Share \\ Capital} \head{$+$ \\} \head{Retained \\ Earnings}
  \end{tabular}
}
\end{document}

在此处输入图片描述

答案2

例如,如果您选择了这条路线,您可能会尝试类似这样的操作tcolorbox。此代码定义了一个新命令\mybox,该命令接受一个可选参数和两个强制参数。(这是基于tcolorbox手册的示例。)可选参数指定主颜色。第一个强制参数指定标题,第二个指定框的主体。然后,您可以使用正常tabular环境或任何您喜欢的环境来设置框主体部分的内容。\myinterbox只接受一个参数,即您想要在两个框之间使用的符号。

\documentclass[landscape]{article}
\usepackage{geometry}
\usepackage{tcolorbox}

\newtcbox{mybox}[2][black]{%
  colback=#1!10!white, colbacktitle=#1!10!white, coltitle=#1!70!black, title={#2}, fonttitle=\bfseries\sffamily, center title, center upper, colupper=#1!70!black, fontupper=\bfseries\sffamily, arc=0pt, boxrule=0pt, titlerule=.5mm, leftupper=0pt, rightupper=0pt, bottom=0pt, top=0pt, nobeforeafter}
\newcommand*{\myinterbox}[1]{%
  \tcbox[colback=white, center upper, colupper=black, fontupper=\bfseries\sffamily, arc=0pt, boxrule=0pt, titlerule=0pt, leftupper=0pt, rightupper=0pt, bottom=0pt, top=0pt, nobeforeafter]{\begin{tabular}{c}#1\\#1\\\mbox{ }\\\end{tabular}}}


\begin{document}

\noindent\mybox[green]{ASSETS}{%
  \setlength{\tabcolsep}{1pt}%
  \begin{tabular}{*{9}{c}}
    & + & Accounts & + & Prepaid & + & Equipment & + & Truck\\
   Cash & & Receivable & & Insurance & & & &
  \end{tabular}}
\myinterbox{=}
\mybox[blue]{LIABILITIES}{%
  \setlength{\tabcolsep}{1pt}%
  \begin{tabular}{*{5}{c}}
    Bank & + & Accounts & + & Unearned\\
    Loan & & Payable & & Revenue
  \end{tabular}}
\myinterbox{+}
\mybox[red]{EQUITY}{%
  \setlength{\tabcolsep}{1pt}%
  \begin{tabular}{*{3}{c}}
    Share & + & Retained\\
    Capital & & Earnings
  \end{tabular}}

\end{document}

彩色盒子

相关内容