4

Here is my setup:

\documentclass[12pt]{report}% Not sure what this is for
 \usepackage{array,ltablex,makecell}%
\renewcommand\theadfont{\normalsize\bfseries}
% \renewcommand*\descriptionlabel[1]{\hspace\leftmargin$#1$}%This is for descriptions to appear on the LHS with an indent
\newenvironment{conditions}
 {\par\vspace{\abovedisplayskip}\noindent\begin{tabular}{>{$}l<{$} @{${}={}$} l}}
 {\end{tabular}\par\vspace{\belowdisplayskip}}%This is for descriptions of equations
\usepackage[]{multirow}%Essential for cells in table spanning multiple rows
\usepackage[autostyle]{csquotes}% This is for quotes
% \usepackage{tabulary}% This is for tables
\usepackage{longtable,array,ragged2e}% This is formatting for long tables
\usepackage{tabularx,caption}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}%This is a wrapper to make everything a certain width -left aligned columns with stuff at the top.
\newcolumntype{U}{>{\raggedright\arraybackslash\hspace{0pt}}X}%This is a wrapper to make everything a certain width -left aligned columns with stuff at the top.
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}%C is for central aligned columns and middle aligned, p puts stuff at the bottom, m middles everything.
\newcolumntype{Y}{>{\centering\arraybackslash\hspace{0pt}}X}%C is for central aligned columns and middle aligned.
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
% \newcommand{\specialcell}[2][c]{\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}%This is to force new lines in cells
\usepackage{graphicx}% This is for images
\usepackage{booktabs,dcolumn,caption}
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}%This is for precision tables per property
%
\begin{document}

    \newsavebox{\sentence}
  \savebox{\sentence}{\parbox[t][][t]{6.5cm}{\RaggedRight\small``Countries with higher GDP per capita at the lower end of the scale include Azerbaijan (around \$7,500), and right at the top we find Luxembourg (\$ 122,147).''}}
  \newlength{\sentenceHeight}
  \setlength{\sentenceHeight}{\totalheightof{\usebox{\sentence}}}

\begin{table}[!ht]
\small

    \newlength{\customspacing}
    \setlength{\customspacing}{.25\sentenceHeight-\baselineskip}
    \centering
    \begin{tabular}{ |C{6.5cm}|C{3.25cm}|C{3.25cm}| }
     \hline
      \textbf{Sentence} & \multicolumn{2}{@{}C{6.5cm}|}{\textbf{Training Examples}} \\ \hline
      \multirow{4}{*}{\usebox{\sentence}} 
      &
      \multicolumn{2}{C{6.5cm}|} {\textbf{Sentence 1}} \\ \cline{2-3}
      &
\multicolumn{2}{C{6.5cm}|}{``Countries with higher GDP per capita at the lower end of the scale include \texttt{LOCATION\_SLOT} (around \$\texttt{NUMBER\_SLOT}), and right at the top we find Luxembourg (\$ 122,147).''}  \\[\customspacing] \cline{2-3}
& \textbf{Region} & \textbf{Value} \\ \cline{2-3}
      & Azerbaijan & 7500 \\ \cline{2-3}
       &
      \multicolumn{2}{C{6.5cm}|} {\textbf{Sentence 2}} \\ \cline{2-3}
            &
\multicolumn{2}{C{6.5cm}|}{``Countries with higher GDP per capita at the lower end of the scale include \texttt{LOCATION\_SLOT} (around \$7,500), and right at the top we find Luxembourg (\$ \texttt{NUMBER\_SLOT}).''}  \\[\customspacing] \cline{2-3}
& \textbf{Region} & \textbf{Value} \\ \cline{2-3}
      & Azerbaijan & 122147 \\ \cline{2-3}
       &
      \multicolumn{2}{C{6.5cm}|} {\textbf{Sentence 3}} \\ \cline{2-3}
            &
\multicolumn{2}{C{6.5cm}|}{``Countries with higher GDP per capita at the lower end of the scale include Azerbaijan (around \$7,500), and right at the top we find \texttt{LOCATION\_SLOT} (\$ \texttt{NUMBER\_SLOT}).''}  \\[\customspacing] \cline{2-3}
& \textbf{Region} & \textbf{Value} \\ \cline{2-3}
      & Luxembourg & 122147 \\ \cline{2-3}
       &
      \multicolumn{2}{C{6.5cm}|} {\textbf{Sentence 4}} \\ \cline{2-3}
            &
\multicolumn{2}{C{6.5cm}|}{``Countries with higher GDP per capita at the lower end of the scale include Azerbaijan (around \$\texttt{NUMBER\_SLOT}), and right at the top we find Luxembourg (\$ 122,147).''}  \\[\customspacing] \cline{2-3}
& \textbf{Region} & \textbf{Value} \\ \cline{2-3}
      & Luxembourg & 7500 \\ \cline{2-3}
      \hline 
    \end{tabular}
    \caption{Multiple training examples from single sentences}
    \label{tbl:multipletrainingexamples}
    \normalsize
  \end{table}

\end{document}

Which currently compiles to:

enter image description here

As you can see, the first cell appears at the top of the cell rather than at the middle.

How can I achieve this? As you can see I have tried placing [t] in the parbox to no avail.

  • 1
    What is the \sentenceHeight length? And the larray package? The \sentence command? – Bernard Sep 5 '16 at 22:59
  • 1
    Edited - apologies. I don't have an larray package loaded? – Dhruv Ghulati Sep 5 '16 at 23:02
  • 1
    No, sorry. It's my fault. I removed a few double package loading, and left a trailing l. – Bernard Sep 5 '16 at 23:20
  • Why do you use t which will align it to the top? – cfr Sep 6 '16 at 0:04
  • Try m rather than p in the definition of C ? – cfr Sep 6 '16 at 0:05
6

The trick for \multirow is to count the number of lines, not the number of rows, and apply a small correction if necessary. I tried to make your table more pleasant to read with some coloured cells, and I removed the duplicate loading of some packages (for instance, ltablex loads both \longtable and tabularx.

\documentclass[12pt]{report}% Not sure what this is for
 \usepackage{array,ltablex,makecell}%
\renewcommand\theadfont{\normalsize\bfseries}
% \renewcommand*\descriptionlabel[1]{\hspace\leftmargin$#1$}%This is for descriptions to appear on the LHS with an indent
\newenvironment{conditions}
 {\par\vspace{\abovedisplayskip}\noindent\begin{tabular}{>{$}l<{$} @{${}={}$} l}}
 {\end{tabular}\par\vspace{\belowdisplayskip}}%This is for descriptions of equations
\usepackage[]{multirow}%Essential for cells in table spanning multiple rows
\usepackage[autostyle]{csquotes}% This is for quotes
% \usepackage{tabulary}% This is for tables
\usepackage{ragged2e}% This is formatting for long tables%larray,
\usepackage{caption}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}%This is a wrapper to make everything a certain width -left aligned columns with stuff at the top.
\newcolumntype{U}{>{\raggedright\arraybackslash\hspace{0pt}}X}%This is a wrapper to make everything a certain width -left aligned columns with stuff at the top.
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}%C is for central aligned columns and middle aligned, p puts stuff at the bottom, m middles everything.
\newcolumntype{Y}{>{\centering\arraybackslash\hspace{0pt}}X}%C is for central aligned columns and middle aligned.
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\usepackage{graphicx}% This is for images
\usepackage{booktabs,dcolumn,caption}
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}%This is for precision tables per property
\usepackage[table, svgnames]{xcolor}%
\usepackage{hhline}%
 \def\sentence{\RaggedRight``Countries with higher GDP per capita at the lower end of the scale include Azerbaijan (around \$\,7,500), and right at the top we find Luxembourg (\$\,122,147).''}
%
\begin{document}

\begin{table}[!ht]
\small
    \centering
    \begin{tabular}{|C{6.5cm}|C{3.25cm}|C{3.25cm}|}
     \hline
      \textbf{Sentence} & \multicolumn{2}{@{}C{6.5cm}|}{\textbf{Training Examples}} \\%
      \hline
      \multirow{32}{\linewidth}[-1pt]{\sentence}%
      &
      \multicolumn{2}{C{\dimexpr6.5cm+2\tabcolsep+\arrayrulewidth}|} {\cellcolor{Lavender!80!lightgray}\textbf{Sentence 1}} \\
      \cline{2-3}
      &
\multicolumn{2}{C{6.5cm}|}{``Countries with higher GDP per capita at the lower end of the scale include \texttt{LOCATION\_SLOT} (around \$\texttt{NUMBER\_SLOT}), and right at the top we find Luxembourg (\$ 122,147).''} \\%
\cline{2-3}
& \textbf{Region} & \textbf{Value} \\ \cline{2-3}
      & Azerbaijan & 7500 \\
\hhline{~|--|}
       &
      \multicolumn{2}{C{\dimexpr6.5cm+2\tabcolsep+\arrayrulewidth}|}{\cellcolor{Lavender!80!lightgray}\textbf{Sentence 2}} \\
      \cline{2-3}
            &
\multicolumn{2}{C{6.5cm}|}{``Countries with higher GDP per capita at the lower end of the scale include \texttt{LOCATION\_SLOT} (around \$7,500), and right at the top we find Luxembourg (\$ \texttt{NUMBER\_SLOT}).''} \\ \cline{2-3}
& \textbf{Region} & \textbf{Value} \\ \cline{2-3}
      & Azerbaijan & 122147 \\
\hhline{~|--|}
       &
      \multicolumn{2}{C{\dimexpr6.5cm+2\tabcolsep+\arrayrulewidth}|}{\cellcolor{Lavender!80!lightgray}\textbf{Sentence 3}} \\%
      \cline{2-3}
 &
\multicolumn{2}{C{6.5cm}|}{``Countries with higher GDP per capita at the lower end of the scale include Azerbaijan (around \$7,500), and right at the top we find \texttt{LOCATION\_SLOT} (\$ \texttt{NUMBER\_SLOT}).''} \\ \cline{2-3}
& \textbf{Region} & \textbf{Value} \\ \cline{2-3}
      & Luxembourg & 122147 \\
\hhline{~|--|}
       &
      \multicolumn{2}{C{\dimexpr6.5cm+2\tabcolsep+\arrayrulewidth}|} {\cellcolor{Lavender!80!lightgray}\textbf{Sentence 4}} \\
      \cline{2-3} &
\multicolumn{2}{C{6.5cm}|}{``Countries with higher GDP per capita at the lower end of the scale include Azerbaijan (around \$\texttt{NUMBER\_SLOT}), and right at the top we find Luxembourg (\$ 122,147).''} \\ \cline{2-3}
& \textbf{Region} & \textbf{Value} \\ \cline{2-3}
      & Luxembourg & 7500 \\ \cline{2-3}
      \hline
    \end{tabular}
    \caption{Multiple training examples from single sentences}
    \label{tbl:multipletrainingexamples}
    \normalsize
  \end{table}

\end{document} 

enter image description here

| improve this answer | |
  • This is exactly what I needed! I will remember this method for multirow next time. Question, what does \hhline{~|--|} have the effect of? – Dhruv Ghulati Sep 6 '16 at 10:16
  • 1
    That is because the \clines disappears with coloured cells. This doesn't happen with \hhlines. hhline is a package usually used for double lines crossing single or double vlines in a smart way, but it can also manage single horizontal lines. Its syntax describes horizontal lines cell by cell: ~ means no line, - a single line and = a double line. See the (short) documentation for more details. – Bernard Sep 6 '16 at 10:44
3

Edit: According to @cfr comment, there is high probability, that I misunderstand the question. So, to be on safe side, I add solution for both cases. Difference between them is only in number of lines, which had to span multirow Lines is 5 in case of text position on the top of column, and 32 in it should be in the middle. Since the multirow is not very accurate to calculate exactly the middle of column height, it provide possibility of manual adjusting of position. Instead to use this, I rather consider less number of lines (4 and 30 respectively).

Difference between both solution is indicated with comments in MWE below.

First solution: Your MWE already generate showed desired table layout. So I'm in doubt, what is the question. Anyway, I try to clean-up your code all duplicated packages and for MWE deleted all, which are not needed for table. Also I add few new packages. From them showframe is only to show page layout. I also suggest to use tabularx table environment (since your table is wider from text width).

I didn't succeed to figured out purpose of some your length. They only cause mess in table (some rows become overwritten). To be table more clear, I introduce two new commands for multicolumn, namely \mcc and mcx. They purpose is self-explanatory from their definition and use in my MWE.

Obtained result for the first solution (text on the top of the first column):

enter image description here

and for second (with vertical centered text in the first column):

enter image description here

and MWE:

\documentclass[12pt]{report}
% packages for tables
\usepackage{makecell,multirow,tabularx}
\renewcommand\theadfont{\bfseries}
\newcolumntype{C}{>{\centering\arraybackslash}X} % <-- new definition for C,
                                                 % now is based on "X" tabularx column
\newcommand\mcx[1]{\multicolumn{2}{%
            >{\setlength\hsize{0.5\textwidth}}X|}{#1}}% <-- new
\newcommand\mcc[1]{\multicolumn{2}{c}{#1}}% <-- new
\usepackage{ragged2e}%
\usepackage{caption}

\usepackage{calc}% <-- added
\usepackage{showframe}% <-- to show page layout, in real document had to be deleted
\newsavebox{\sentence}% <-- moved here

    \begin{document}
\begin{table}[!ht]
    \small
    \centering
    \renewcommand{\arraystretch}{1.3}
\savebox{\sentence}{\parbox{0.5\textwidth-2\tabcolsep}{%
    \RaggedRight
    ``Countries with higher GDP per capita at the lower end of the scale
    include Azerbaijan (around \$7,500), and right at the top we find
    Luxembourg (\$ 122,147).''}}

\begin{tabularx}{\textwidth}{|>{\setlength\hsize{0.50\hsize}}X|
                              >{\setlength\hsize{0.25\hsize}}C|
                              >{\setlength\hsize{0.25\hsize}}C|}
     \hline
\thead{Sentence}    
    &   \mcc{\thead{Training Examples}}         \\  \hline
\multirow{32}{*}{\usebox{\sentence}}% <-- multirow span 32 lines ...
                                    % if the question is, how to center text
                                    % in the first column.
%\multirow{4}{*}{\usebox{\sentence}}% <-- the case, that text should be on the top
                                % of the column, instead of 30 lines, the
                                % multirow should span only 4
    &   \mcc{\textbf{Sentence 1}}               \\  \cline{2-3}
    &   \mcx{``Countries with higher GDP per capita at the lower end of the
             scale include \texttt{LOCATION\_SLOT} (around \$\texttt{NUMBER\_SLOT}), 
             and right at the top we find Luxembourg (\$ 122,147).''} \\  \cline{2-3}
    &   \textbf{Region}     &   \textbf{Value}  \\  \cline{2-3}
    &   Azerbaijan          &   7500            \\  \cline{2-3}
    &   \mcc{\textbf{Sentence 2}}               \\  \cline{2-3}
    &   \mcx{``Countries with higher GDP per capita at the lower end of the
             scale include \texttt{LOCATION\_SLOT} (around \$7,500), and
             right at the top we find Luxembourg (\$ \texttt{NUMBER\_SLOT}).''}   \\  \cline{2-3}
    &   \textbf{Region}     &   \textbf{Value}  \\  \cline{2-3}
    &   Azerbaijan          &   122147          \\  \cline{2-3}
    &   \mcc{\textbf{Sentence 3}}               \\  \cline{2-3}
    &   \mcx{``Countries with higher GDP per capita at the lower end of the
             scale include Azerbaijan (around \$7,500), and right at the top
             we find \texttt{LOCATION\_SLOT} (\$ \texttt{NUMBER\_SLOT}).''}      \\  \cline{2-3}
    &   \textbf{Region}     &   \textbf{Value}  \\  \cline{2-3}
    &   Luxembourg          &   122147          \\  \cline{2-3}
    &   \mcc{\textbf{Sentence 4}}               \\  \cline{2-3}
    &   \mcx{``Countries with higher GDP per capita at the lower end of the
             scale include Azerbaijan (around \$\texttt{NUMBER\_SLOT}), 
             and right at the top we find Luxembourg (\$ 122,147).''}            \\  \cline{2-3}
    &   \textbf{Region}     & \textbf{Value}    \\  \cline{2-3}
    &   Luxembourg          & 7500              \\  \hline
\end{tabularx}
    \caption{Multiple training examples from single sentences}
\label{tbl:multipletrainingexamples}
  \end{table}
\end{document}
| improve this answer | |
  • This doesn't answer the question. The image there is what the OP now gets and not what the OP wants to get. – cfr Sep 6 '16 at 0:26
  • Thank you for clarification. I didn't figured out, what is the problem., I will correct t this, is just meter of number of rows considered in multirow ... and than evenually delete answer ... – Zarko Sep 6 '16 at 0:30
  • It wasn't very clear, unfortunately. You are not the only person to interpret this way. (2/3 answers are based on this!) – cfr Sep 6 '16 at 0:32
  • Actually, on second thoughts maybe you shouldn't change it. The subject says one thing and the question another. I would leave this until the OP clarifies. Sorry for the confusion. – cfr Sep 6 '16 at 0:34
1

\totalheightof command is present in the calc package which is not referenced. try inserting \usepackage{calc} just before \begin{document} and recompile. Here is the output.

expected output

| improve this answer | |
  • Welcome! I'm afraid this doesn't answer the question. The image there is what the OP now gets and not what the OP wants to get. (But you are not the only one to make this mistake - see Zarko's answer, which reflects the same interpretation.) – cfr Sep 6 '16 at 0:27
  • Actually, on second thoughts maybe you shouldn't change it. The subject says one thing and the question another. I would leave this until the OP clarifies. Sorry for the confusion. – cfr Sep 6 '16 at 0:34
  • You can highlight in-line code in your post using back-ticks (rather than <b>...</b>. To highlight code-blocks, either indent them by four spaces or use the {} on the gui. – Andrew Sep 6 '16 at 0:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.