创造一个漂亮的桌子

创造一个漂亮的桌子

我想创建一个带有精美外框的表格。我认为使用以下示例是个好主意texample.net

问题是不可能在 tikzpicture 中使用浮点数,另一个更大的问题是,我想以一种在其中出现的方式设置我的标题,fancytitle但它应该是我使用的“真实”标题\listoftables

$\clubsuit$如果它很重要:在这个例子中,框右侧有这个。在我的应用程序中将有一个自定义的小 tikzpicture。

不使用 tikz 可以实现此目的吗?无论如何,这可能吗?

平均能量损失

\documentclass{article}
\usepackage{longtable}
\usepackage{caption}

% tikz example taken from http://www.texample.net/tikz/examples/boxes-with-text-and-math/
\usepackage{tikz}
\usetikzlibrary{shapes,snakes}
% Define box and box title style
\tikzstyle{mybox} = [draw=red, fill=blue!20, very thick, rectangle, rounded corners, inner sep=10pt, inner ysep=20pt]
\tikzstyle{fancytitle} = [fill=red, text=white]
\begin{document}

\listoftables

  \begin{tikzpicture}
    \node [mybox] (box){%
    % put table here
    %  \begin{table}
    %    \caption{the cpation}
    %    \centering
    %    \begin{tabular}{l l}
    %      a &b \\
    %      b &d
    %    \end{tabular}
    %    \label{tab:tab}
    %  \end{table}
    };
    \node[fancytitle, right=10pt] at (box.north west) {the fancy caption}; % put caption here, it must be a "real" caption since I need the cpation for \listoftables
    \node[fancytitle, rounded corners] at (box.east) {$\clubsuit$};
  \end{tikzpicture}%
\end{document}

所需输出

wanted output

答案1

这是使用 Tikz 和包的解决方案caption。我还添加了一个常规表格来向您展示它们在文本中的显示方式。花式表格的新命令如下:

\fancytab[ <optional> ]{ <label> }{ <caption> }{ <table goes here> }

虽然所有参数都是不言自明的,但第一个可选参数仅接受\centering或其他对齐命令,例如\flushright,其他任何命令都可能出错或根本不起作用。如果忽略此参数,表格将显示为左对齐。

我已经尝试测试该命令的稳健性,并且一切似乎都井然有序,但如果您注意到某些命令中断的情况,请告诉我。

输出(两页)

enter image description here

enter image description here

代码

\documentclass{article}
\usepackage{tikz}
\usepackage{caption}
\usepackage{geometry}
\usepackage{booktabs}

\usetikzlibrary{calc, backgrounds}

\newcommand\fancytab[4][]{%
\vspace*{\baselineskip}\noindent
\begin{minipage}{\linewidth}%
\captionsetup[table]{format=plain,skip=0pt,indention=0pt, font={color=white}}
\newlength{\mywidth}
\settowidth{\mywidth}{Table~n:~#3+2}
#1
\begin{tikzpicture}
\node (around) {%
#4
};
\begin{scope}[on background layer]
\draw[red, rounded corners, thick, fill=blue!10] ($(around.north west)+(0,5mm)$) rectangle (around.south east);
\node[fill=red, anchor=west, xshift=3mm, text width=\mywidth] at ($(around.north west)+(0,5mm)$) {\captionof{table}{#3}};
\node[text=white, fill=red] at (around.east) {$\clubsuit$};
\end{scope}
\end{tikzpicture}
\label{tab:#2}
\end{minipage}\par
\vspace*{\baselineskip}
\let\mywidth\relax % so we can reuse the same length
}

\begin{document}

\listoftables
\newpage

\begin{table}[!htb]
\centering
\caption{One caption}
\begin{tabular}{|r|l|}
  \hline
  7C0 & hexadecimal \\
  3700 & octal \\ \cline{2-2}
  11111000000 & binary \\
  \hline \hline
  1984 & decimal \\
  \hline
\end{tabular}
\end{table}

Nullam libero arcu, placerat et eleifend nec, tincidunt vel eros. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec vel dui pulvinar, malesuada mauris ac, finibus neque. Phasellus imperdiet posuere sodales. Sed feugiat quis lorem id laoreet.

\fancytab{tabnew}{A caption}{%
\begin{tabular}{rl}
7C0 & hexadecimal \\
3700 & octal \\
11111000000 & binary \\
\midrule
1984 & decimal \\
\end{tabular}
}

Quisque blandit pretium suscipit. Nulla eget lectus rhoncus, maximus turpis sed, mattis sapien. Sed scelerisque metus dolor, sed elementum nisi laoreet eget. Aenean non tempus arcu. Praesent pharetra diam ut dictum dapibus. Etiam maximus orci at magna efficitur bibendum.

\fancytab[\centering]{tab}{A much much longer caption}{%
\begin{tabular}{l*{6}{c}r}
Team              & P & W & D & L & F  & A & Pts \\
\midrule
Manchester United & 6 & 4 & 0 & 2 & 10 & 5 & 12  \\
Celtic            & 6 & 3 & 0 & 3 &  8 & 9 &  9  \\
Benfica           & 6 & 2 & 1 & 3 &  7 & 8 &  7  \\
FC Copenhagen     & 6 & 2 & 1 & 3 &  5 & 8 &  7  \\
\end{tabular}
}
\end{document} 

答案2

下面的代码展示了一个解决方案tcolorbox包装。正如 percusse 所建议的,这个包装可以轻松制作这种精美的盒子。

借助blend into初始化选项,将 tcolorboxes 集成到图形或表格列表中也非常容易:标题用作caption并集成到 LOT 中,编号跟随table计数器。

而不是\begin{table} .... \caption{...} \end{table},你使用

\begin{mytable}[optional parameters]{mandatory table caption}
... table contents ....
\end{mytable}

表格内容示例被盗Alenanno 的回答可能:

\documentclass{article}
\usepackage{booktabs}
\usepackage[most]{tcolorbox}

\newtcolorbox[blend into=tables]{mytable}[2][]{%
    enhanced,
    float, 
    every float=\centering,
    capture=hbox, 
    title = #2, 
    attach boxed title to top left={%
        xshift=5mm, 
        yshift=-\tcboxedtitleheight/2, 
        yshifttext=-1mm},
    boxed title style={colback=red, sharp corners},
    colframe = red,
    colback = blue!20,  
    overlay = {\node[text=white, fill=red] at (frame.east) 
        {$\clubsuit$};},
    #1}

\begin{document}
\listoftables

\begin{table}
\centering
\begin{tabular}{|r|l|}
  \hline
  7C0 & hexadecimal \\
  3700 & octal \\ \cline{2-2}
  11111000000 & binary \\
  \hline \hline
  1984 & decimal \\
  \hline
\end{tabular}
\caption{First table}
\end{table}

\begin{mytable}{Second table}
\begin{tabular}{rl}
  7C0 & hexadecimal \\
  3700 & octal \\ 
  11111000000 & binary \\
  \midrule
  1984 & decimal \\
\end{tabular}
\end{mytable}

\begin{mytable}[colback=red!5]{Trird table}
\begin{tabular}{l*{6}{c}r}
Team              & P & W & D & L & F  & A & Pts \\
\midrule
Manchester United & 6 & 4 & 0 & 2 & 10 & 5 & 12  \\
Celtic            & 6 & 3 & 0 & 3 &  8 & 9 &  9  \\
Benfica           & 6 & 2 & 1 & 3 &  7 & 8 &  7  \\
FC Copenhagen     & 6 & 2 & 1 & 3 &  5 & 8 &  7  \\
\end{tabular}
\end{mytable}

\end{document}

enter image description here

相关内容