我要创建一个表格,它看起来像囚徒困境,但我无法创建它。
它看起来应该是这样的:
我的序言如下:
\documentclass[a4paper,12pt,oneside]{article}
\usepackage[english]{babel}
\usepackage{tikz}
\usepackage{pgf}
\usetikzlibrary{shapes,arrows,positioning,calc}
\usepackage{tabu}
\usepackage{hyperref}
\usepackage{longtable}
\usepackage{array}
\usepackage{siunitx,multirow,tabularx,booktabs}
\renewcommand{\arraystretch}{1.5}
\def\mathbi#1{\textbf{\em #1}}
\newcommand{\transp}{^{\mathsf{T}}}
\begin{document}
\begin{table}[h]
\begin{center}
\caption{The Prisoner’s Dilemma}
\begin{tabular}{c | c | c |}
&Factor1&Defect\\\hline%
Factor2&5&4\\
Defect&2&2\\\hline
\end{tabular}
\end{center}
\end{table}
\end{document}
表格如下所示:
有人能告诉我如何制作第一个带有 AND 而不带有额外框的表格吗?单元格大小应该相同。
答案1
使用 TikZ 就简单多了:
代码:
\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,matrix}
\begin{document}
\begin{tikzpicture}[element/.style={minimum width=1.75cm,minimum height=0.85cm}]
\matrix (m) [matrix of nodes,nodes={element},column sep=-\pgflinewidth, row sep=-\pgflinewidth,]{
& Factor 1 & Factor 2 \\
Factor 1 & |[draw]|5 & |[draw]|4 \\
Factor 2 & |[draw]|2 & |[draw]|2 \\ };
\node[draw,element, anchor=west,label={above:\textbf{Name 3}}] at ($(m-2-3)!0.5!(m-3-3)+(1.25,0)$) {5}; % setting the node midway to cell 4 and cell 2 with a horizontal shift of 1.25cm
\node[above=0.25cm] at ($(m-1-2)!0.5!(m-1-3)$){\textbf{Name 1}};
\node[rotate=90] at ($(m-2-1)!0.5!(m-3-1)+(-1.25,0)$){\textbf{Name 2}};
\end{tikzpicture}
\end{document}
版本内article
\documentclass[11pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{calc,matrix}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{table}[h]
\centering
\caption{bla bla bla}
\label{bla}
\begin{tikzpicture}[element/.style={minimum width=1.75cm,minimum height=0.85cm}]
\matrix (m) [matrix of nodes,nodes={element},column sep=-\pgflinewidth, row sep=-\pgflinewidth,]{
& Factor 1 & Factor 2 \\
Factor 1 & |[draw]|5 & |[draw]|4 \\
Factor 2 & |[draw]|2 & |[draw]|2 \\ };
\node[draw,element, anchor=west,label={above:\textbf{Name 3}}] at ($(m-2-3)!0.5!(m-3-3)+(1.25,0)$) {5}; % setting the node midway to cell 4 and cell 2 with a horizontal shift of 1.25cm
\node[above=0.25cm] at ($(m-1-2)!0.5!(m-1-3)$){\textbf{Name 1}};
\node[rotate=90] at ($(m-2-1)!0.5!(m-3-1)+(-1.25,0)$){\textbf{Name 2}};
\end{tikzpicture}
\end{table}
\lipsum[2]
\end{document}
结果:
解释
TikZ 矩阵只是一个节点,每个节点都会有一些可用的锚点:顺便说一下,这对定位图片中的其他元素有很大帮助。
第一步是给矩阵命名:通过语法完成,\matrix (m)
其中m
是名称。使用选项matrix of nodes
,则每个单元格也都有一个名称,形式为<matrix name>-<row>-<column>
。
由于我们有一个名称,因此我们可以将锚点作为 来访问<matrix name>.<anchor>
。但我们必须知道这些锚点位于何处。出于调试目的,我们可以在之前的\matrix
定义后立即添加此代码:
% debugging purposes
\draw[red](m.south west)rectangle(m.north east);
\foreach \anchor/\placement in
{north/above, south/below, east/right, west/left,
north west/above, north east/above,
south west/below, south east/below}
\draw[shift=(m.\anchor)] plot[mark=x] coordinates{(0,0)} node[\placement=0.15cm]{\scriptsize\texttt{(m.\anchor)}};
现在表格如下所示:
图中有两个注释:它们被添加
% Where to put the notes according to what we learnt about matrices
\node[below=0.5cm] at (m.south west) {Note};
\node[below=0.5cm, align=center,text width=5cm] at (m.south) {Another Note which is supposed to be longer};
答案2
有一个用于排版战略形式游戏的包:sgame
,作者:马丁·奥斯本。
代码
\documentclass{article}
\usepackage{sgame}
% \renewcommand\gamestretch{2} % double row height
\begin{document}
\begin{game}{2}{2}[\textbf{Name 2}][\textbf{Name 1}]
& Factor 1 & Factor 2 \\
Factor 1 & 5 & 4 \\
Factor 2 & 2 & 2
\end{game}
\quad
\begin{game}{1}{1}
& \textbf{Name 3} \\
& 1
\end{game}
\end{document}
输出
答案3
您可以使用\cline
说明并\multicolumn{1}{c}{...}
在标题行中使用来抑制垂直线。我提供了一些视觉格式挂钩(例如下面 MWE 中的\quad
和\lower
;我相信您将设法弄清楚如何使用这些挂钩来移动元素。
\documentclass[a4paper,12pt]{article}
\usepackage{graphicx,multirow}
\begin{document}
\begin{table}[h]
\renewcommand\arraystretch{2.5} % provide a bit taller rows
\centering
\caption{The Prisoner's Dilemma}
\begin{tabular}{ll | c | c |}
&\multicolumn{1}{c}{}&\multicolumn{2}{c}{\textbf{Name1}}\\[-2ex]
&\multicolumn{1}{c}{}
&\multicolumn{1}{c}{Factor1}&\multicolumn{1}{c}{Factor2}\\
\cline{3-4}
\multirow{2}{*}{\rotatebox{90}{\textbf{Name2}}}
&Factor1&5&4\\
\cline{3-4}
&Factor2&2&2\\
\cline{3-4}
\end{tabular}
\qquad
\lower3ex\hbox{\begin{tabular}{|c|}
\multicolumn{1}{c}{\textbf{Name3}}\\
\hline
5\\
\hline
\end{tabular}}
\end{table}
\end{document}
附录:如果您有两个宽度不同的列标题(例如ExtremelyLongHeader
和LilHeader
),并且这些标题的宽度决定了整体列宽,则可以按如下方式均衡列宽:
在序言中插入以下说明:
\newcommand\LongestHeader{ExtremelyLongHeader} % replace "ExtremelyLongHeader" with actual header... \newlength\mylen \settowidth\mylen{\LongestHeader} % \mylen measures width of \LongestHeader
要设置
tabular
环境的标题,您需要输入:\begin{tabular}{ll | c | c |} & \multicolumn{1}{c}{} &\multicolumn{2}{c}{\textbf{Name1}}\\[-2ex] & \multicolumn{1}{c}{} & \multicolumn{1}{c}{\LongestHeader} & \multicolumn{1}{c}{\parbox{\mylen}{\centering LilHeader}}\\ % replace "LilHeader" with real column header \cline{3-4} ...
而不仅仅是
\begin{tabular}{ll | c | c |} & \multicolumn{1}{c}{}&\multicolumn{2}{c}{\textbf{Name1}}\\[-2ex] & \multicolumn{1}{c}{} & \multicolumn{1}{c}{ExtremelyLongHeader} & \multicolumn{1}{c}{LilHeader}\\ \cline{3-4} ...
该
\parbox
命令用于排版预定宽度的“段落框”。