2

I have a table that uses tabularx with some p columns and some X columns. In the p columns I manually added line breaks. Everything is fine except the fact that the vertical lines that divide the columns from each other are broken at the places where I added the line breaks. What is the best way to fix this? This MWE shows the problem:

\documentclass[xcolor=dvipsnames, mode=presentation]{beamer}
\usepackage{tabularx}
\begin{document}
\begin{frame}
\frametitle{Results}
\begin{columns} \column{\dimexpr\paperwidth-10pt} \scriptsize
\begin{tabularx}{\textwidth}{|p{0.24\textwidth}|p{0.09\textwidth}|X|X|p{0.09\textwidth}|X|X|}
    \hline
    \textbf{settings} & some result & some result & some result & some result & some result & some result \\ \hline
    \textbf{cat1:} bla & 1 & 1 & 1 & 1 & 1 & 1 \\ \hline
    \textbf{cat1:} lala & 1 & 1 & 1 & 1 & 1 & 1 \\ \hline
    \textbf{cat1:} bla,\\ \textbf{cat2:} some value & 1 & 1 & 1 & 1 & 1 & 1 \\ \hline
    \textbf{cat1:} bla,\\ \textbf{cat2:} some value,\\ \textbf{cat3:} other value & 1 & 1 & 1 & 1 & 1 & 1 \\ \hline
\end{tabularx}
\end{columns}
\end{frame}
\end{document}
0

1 Answer 1

3

Use \newline instead of \\, if you mean it.

\documentclass[xcolor=dvipsnames, mode=presentation]{beamer}
\usepackage{tabularx}
\begin{document}
\begin{frame}
\frametitle{Results}
\begin{columns} \column{\dimexpr\paperwidth-10pt} \scriptsize
\begin{tabularx}{\textwidth}{|p{0.24\textwidth}|p{0.09\textwidth}|X|X|p{0.09\textwidth}|X|X|}
    \hline
    \textbf{settings} & some result & some result & some result & some result & some result & some result \\ \hline
    \textbf{cat1:} bla & 1 & 1 & 1 & 1 & 1 & 1 \\ \hline
    \textbf{cat1:} lala & 1 & 1 & 1 & 1 & 1 & 1 \\ \hline
    \textbf{cat1:} bla,\newline \textbf{cat2:} some value & 1 & 1 & 1 & 1 & 1 & 1 \\ \hline
    \textbf{cat1:} bla,\newline \textbf{cat2:} some value,\newline \textbf{cat3:} other value & 1 & 1 & 1 & 1 & 1 & 1 \\ \hline
\end{tabularx}
\end{columns}
\end{frame}
\end{document}

enter image description here

Or define a more versatile macro:

\newcommand{\mycell}[2][t]{%
\begin{tabular}[#1]{@{}l@{}}
#2
\end{tabular}
}
%% #1 - alignment, t, or c or b, #2 - content.

so that you can even control the vertical alignment.

\documentclass[xcolor=dvipsnames, mode=presentation]{beamer}
\usepackage{tabularx}
\newcommand{\mycell}[2][t]{%
\begin{tabular}[#1]{@{}l@{}}
#2
\end{tabular}
}
\begin{document}
\begin{frame}
\frametitle{Results}
\begin{columns} \column{\dimexpr\paperwidth-10pt} \scriptsize
\begin{tabularx}{\textwidth}{|p{0.24\textwidth}|p{0.09\textwidth}|X|X|p{0.09\textwidth}|X|X|}
    \hline
    \textbf{settings} & some result & some result & some result & some result & some result & some result \\ \hline
    \textbf{cat1:} bla & 1 & 1 & 1 & 1 & 1 & 1 \\ \hline
    \textbf{cat1:} lala & 1 & 1 & 1 & 1 & 1 & 1 \\ \hline
    \mycell{\textbf{cat1:} bla,\\ \textbf{cat2:} some value} & 1 & 1 & 1 & 1 & 1 & 1 \\ \hline
    \mycell[c]{\textbf{cat1:} bla,\\ \textbf{cat2:} some value,\\ \textbf{cat3:} other value} & 1 & 1 & 1 & 1 & 1 & 1 \\ \hline
\end{tabularx}
\end{columns}
\end{frame}
\end{document}

enter image description here

5
  • oh, that hyphenated word in the headings is unattractive, especially since in the columns with a different format than X, the space before "re-" is wider Commented Oct 24, 2015 at 15:05
  • @HarishKumar Thank you, using \newline did the trick. Can you explain why? Also, I don't understand your macro. Can you give more explanation to that?
    – anjuta
    Commented Oct 24, 2015 at 18:24
  • @BarbaraBeeton My real table looks different and those values are perfect for it. The table I posted here is just an example.
    – anjuta
    Commented Oct 24, 2015 at 18:25
  • 1
    @anjuta: Inside tabular ` \\ ` is meant for row breaking. If you use it inside the cell like you did, it does only bad. i.e., you are creating 2 extra rows with only one cell, so no vertical lines are drawn.So we use the equivalent, \newline which means new line.
    – user11232
    Commented Oct 24, 2015 at 23:37
  • 1
    @anjuta: The new macro is a simple tabular with a single l column. I used @{} to remove the padding on both sides of this column. And #1 specifies the alignment point of the tabular.
    – user11232
    Commented Oct 24, 2015 at 23:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .