我如何使用 TikZ(或其他库)绘制如下内容:
答案1
另一种方法是使用 TikZ,其中结构是手动构建的。为了方便起见,将其包装在宏中,第二列的宽度设置为该列中最宽条目的宽度。
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,backgrounds}
\newcommand\someboxthingy[3]{
\pgfmathsetmacro\widestentry{max(\widthof{#1},\widthof{#2},\widthof{#3})}
\begin{scope}[
lcol/.style={anchor=east},
rcol/.style={anchor=base west,align=left,text width=\widestentry,fill=white,draw=black},
node distance=4.5ex and 2pt]
\node (from) {From:};
\node [below=of from.east,lcol] (to) {To:};
\node [below=of to.east,lcol] (subj) {Subject:};
\node [right=of from.base east,rcol] (ur) {#1};
\node [right=of to.base east,rcol] {#2};
\node [right=of subj.base east,rcol] {#3};
\end{scope}
\begin{scope}[on background layer]
\node[fill=black!20,draw=black,fit=(subj)(ur)] {};
\end{scope}
}
\begin{document}
\begin{tikzpicture}
\someboxthingy{Lycke}{Jean}{On the procurement of money}
\end{tikzpicture}
\end{document}
答案2
通过表格解决的优点是宽度可以自动设置为所需的空间:
\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\begin{document}
\setlength{\fboxrule}{1pt}
\setlength{\fboxsep}{\tabcolsep}
\setlength{\arrayrulewidth}{1pt}
\fcolorbox{black}{lightgray}{%
\begin{tabular}{
@{}% margin from outer \fcolorbox
r
% vertical line, which is moved to the right
!{\vrule width\arrayrulewidth\kern-\arrayrulewidth}
% set cell color:
>{\columncolor{white}
[\dimexpr\tabcolsep-\arrayrulewidth\relax]
[\tabcolsep]}
l|
}
\cline{2-2}
\noalign{\kern\arrayrulewidth}
From: & Chris\\
\cline{2-2}
\addlinespace
\cline{2-2}
\noalign{\kern\arrayrulewidth}
To: & Albert, Eddie\\
\cline{2-2}
\addlinespace
\cline{2-2}
\noalign{\kern\arrayrulewidth}
Subject: & Our presentation for class\\
\cline{2-2}
\end{tabular}%
}
\end{document}
使用了一些技巧:
外部灰色框通过 设置
\fcolorbox
。线条粗细\fboxsep
设置为 1 pt。边距\fboxsep
设置为\tabcolsep
。\columncolor
白色字段通过包设置,通过包的colortbl
选项加载。table
xcolor
\addlinespace
包bookmark
设置了不属于单元格的额外行空间,这样可以避免此区域出现白色背景颜色和垂直线。tabular
内框通过垂直线和水平线完成\cline
。后者的特点是它不会向下移动以允许进一步的\cline
命令。向下移动在这里很重要,否则线条会被白色单元格背景颜色覆盖。向下移动是通过 实现的\noalign{\kern\arrayrulewidth}
。左侧垂直线默认在单元格外,边角不好看,因此将线设置在单元格内:
!{\vrule\arrayrulewidth\kern-\arrayrulewidth}
。然后需要指定可选参数 ,\cellcolor
以避免左侧垂直线重叠。规则的线条粗细
tabular
通过 设置\arrayrulewidth
。
答案3
您可以尝试使用matrix
作为填充的背景节点,同时根据您的喜好绘制或填充内部节点。
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[field/.style={draw, fill=white,
minimum height=1cm, text width= 5cm, align=left}]
\matrix (card) [matrix of nodes, draw, fill=gray,
column 2/.style={nodes=field},
column 1/.style={text width=1.5cm, align=right}]
{From: & Chris\\
To: & Albert, Eddy\\
Subject: & FREE OFFER!!!!\\};
\end{tikzpicture}
\end{document}
更新:仍然使用matrix
但现在在\mybox
命令内部并\widestwidth
使用Torbjørn T.以及一些小的调整。
\documentclass{article}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{matrix}
\newcommand{\mybox}[3]{%
\pgfmathsetmacro\widestentry{max(\widthof{#1}, \widthof{#2},\widthof{#3})}
\begin{tikzpicture}[field/.style={draw, fill=white,
text width= \widestentry,
text depth=\depthof{y}, align=left}]
\matrix (card) [matrix of nodes, row sep=3pt, draw,
fill=gray!30, ampersand replacement=\&,
column 2/.style={nodes=field},
column 1/.style={text width=1.2cm, align=right}]
{From: \& {#1}\\ To: \& {#2}\\ Subject: \& {#3}\\};
\end{tikzpicture}}
\begin{document}
\mybox{Chris}{Albert, George, Sue}{Let's make a study group}
\end{document}