5

enter image description here

I wonder how to wrap the column header in last column by specifying the column width in the tabular environment created in tikz. Any help will be highly appreciated. Thanks

\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}
MYaseen208
  • 8,587

1 Answers1

6

You can use a \parbox inside \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}

enter image description here

I also defined a new column type

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

since narrow columns might produce ugly results with inter-word spacing.

Gonzalo Medina
  • 505,128
  • @MYaseen208 You're welcome. I updated my answer introducing a minor modification using a new column type that might be of interest for you. – Gonzalo Medina May 03 '14 at 19:44