在 tikz 中创建的表格环境中的列宽

在 tikz 中创建的表格环境中的列宽

在此处输入图片描述

我想知道如何在 中创建的表格环境中通过指定列宽来将列标题包装在最后一列中tikz。任何帮助都将不胜感激。谢谢

\documentclass[preview]{standalone}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{array}
\usepackage{environ}
\usepackage{tikz}

\usepackage{caption}

\newsavebox{\tablebox}
\definecolor{tablecolor}{named}{ForestGreen}

\NewEnviron{rndtable}[1]{%
  \addtolength{\extrarowheight}{1ex}%
  \rowcolors{2}{tablecolor!20}{tablecolor!40}%
  \sffamily
  \newcommand{\header}[1]{%
    \multicolumn{1}{l}{%
      \cellcolor{tablecolor}%
      \color{white}%
      \bfseries##1%
    }%
  }%
  \savebox{\tablebox}{%
    \begin{tabular}{#1}%
      \BODY
    \end{tabular}%
  }%

  \begin{tikzpicture}
    \begin{scope}
      \clip[rounded corners=1ex]
        (0,-\dp\tablebox) rectangle (\wd\tablebox,\ht\tablebox);
      \node at (0,-\dp\tablebox) [anchor=south west,inner sep=0pt]
            {\usebox{\tablebox}};
    \end{scope}
    \draw[tablecolor,very thick,rounded corners=1ex]
      (0,-\dp\tablebox) rectangle (\wd\tablebox,\ht\tablebox);
  \end{tikzpicture}%
}

\begin{document}
\begin{center}
%\captionof{table}{Caption}
\footnotesize
\begin{rndtable}{l p{4cm}  p{3cm} p{5cm}} %  
  \header{Country} &
  \header{Variable} &
  \header{Targets} &
  \header{Frequency of Setting the Target} \\
  \textbf{Afghanistan} & Currency in circulation (CiC) & Growth rate of CiC & Annual \\
  \textbf{Bangladesh} & Broad money  & M2 growth rate & Annual \\
  \textbf{Bhutan} & Exchange rate & 1BTN=1INR & Irregular \\
  \textbf{India} & Multiple indicators & No specific targets & Annual \\
  \textbf{Maldives} & Exchange rate & 12.85MVR=1USD (+/-20\%) & Irregular \\
  \textbf{Nepal} & Exchange rate & NA & NA \\
  \textbf{Pakistan} & Inflation, together with an assessment of monetary aggregates  & Projections of inflation relative to the announced target and projections of various components of monetary aggregates  & Annual \\
  \textbf{Sri Lanka} & Broad money & Consolidated Broad Money (M2b)  & Annual \\

    \multicolumn{4}{l} {\footnotesize \textcolor{red} {Source: Central banks' websites}}\\
    \multicolumn{4}{l}{\footnotesize \textcolor{red} {Note: Targets are the latest numbers available at the time of compilation}}\\
    \multicolumn{4}{l}{\footnotesize \textcolor{red} {BTN: Bhutanese Ngultrum; INR: Indian Rupee; MVR: Maldives Rufiyaa; USD: US Dollar}}\\
    \end{rndtable}

\end{center}

\end{document}

答案1

您可以使用\parbox内部\header

\documentclass[preview]{standalone}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{array}
\usepackage{environ}
\usepackage{tikz}

\usepackage{caption}

\newsavebox{\tablebox}
\definecolor{tablecolor}{named}{ForestGreen}

\NewEnviron{rndtable}[1]{%
  \addtolength{\extrarowheight}{1ex}%
  \rowcolors{2}{tablecolor!20}{tablecolor!40}%
  \sffamily
  \newcommand{\header}[1]{%
    \multicolumn{1}{l}{%
      \cellcolor{tablecolor}%
      \color{white}%
      \bfseries##1%
    }%
  }%
  \savebox{\tablebox}{%
    \begin{tabular}{#1}%
      \BODY
    \end{tabular}%
  }%
%
  \begin{tikzpicture}
    \begin{scope}
      \clip[rounded corners=1ex]
        (0,-\dp\tablebox) rectangle (\wd\tablebox,\ht\tablebox);
      \node at (0,-\dp\tablebox) [anchor=south west,inner sep=0pt]
            {\usebox{\tablebox}};
    \end{scope}
    \draw[tablecolor,very thick,rounded corners=1ex]
      (0,-\dp\tablebox) rectangle (\wd\tablebox,\ht\tablebox);
  \end{tikzpicture}%
}

\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}

\begin{document}

%\captionof{table}{Caption}
\footnotesize
\begin{rndtable}{l P{4cm}  P{2.8cm} P{2cm}} %  
  \header{Country} &
  \header{Variable} &
  \header{Targets} &
  \header{\parbox[t]{2cm}{Frequency \\ of Setting  \\ \strut the Target}} \\
  \textbf{Afghanistan} & Currency in circulation (CiC) & Growth rate of CiC & Annual \\
  \textbf{Bangladesh} & Broad money  & M2 growth rate & Annual \\
  \textbf{Bhutan} & Exchange rate & 1BTN=1INR & Irregular \\
  \textbf{India} & Multiple indicators & No specific targets & Annual \\
  \textbf{Maldives} & Exchange rate & 12.85MVR=1USD (+/-20\%) & Irregular \\
  \textbf{Nepal} & Exchange rate & NA & NA \\
  \textbf{Pakistan} & Inflation, together with an assessment of monetary aggregates  & Projections of inflation relative to the announced target and projections of various components of monetary aggregates  & Annual \\
  \textbf{Sri Lanka} & Broad money & Consolidated Broad Money (M2b)  & Annual \\

%    \multicolumn{4}{l} {\footnotesize \textcolor{red} {Source: Central banks' websites}}\\
%    \multicolumn{4}{l}{\footnotesize \textcolor{red} {Note: Targets are the latest numbers available at the time of compilation}}\\
%    \multicolumn{4}{l}{\footnotesize \textcolor{red} {BTN: Bhutanese Ngultrum; INR: Indian Rupee; MVR: Maldives Rufiyaa; USD: US Dollar}}\\
    \end{rndtable}

\end{document}

在此处输入图片描述

我还定义了一个新的列类型

\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}

因为窄列可能会导致字间间距产生难看的结果。

相关内容