Graphics, Figures & TablesHow do I automatically add linebreaks to my table?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
rain
Posts: 12
Joined: Thu Jan 28, 2016 9:00 pm

How do I automatically add linebreaks to my table?

Post by rain »

Hello, my table goes out of screen.

Code: Select all

\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|l|l|l|l|}
\hline
\textbf{Criteria} & \textbf{k-Anonymity}                                                                                                                                                                                                                                                                              & \textbf{(X, Y)-Anonymity}                                                                                                                                                                                        & \textbf{Multirelational k-Anonymity}                 \\ \hline
\textbf{Purpose}  & Single table where each row  represents a distinct person.                                                                                                                                                                                                                                        & Single table where one person can appear in multiple rows.                                                                                                                                                       & Multiple relational tables.                          \\ \hline
\textbf{Weakness} & \begin{tabular}[c]{@{}l@{}}Does not guarantee diversity of sensitive attribute.\\ Does not guarantee sufficient privacy when person is present in multiple rows.\\ Does not offer sufficient privacy or renders table useless for dataminers when used on multi relational database.\end{tabular} & \begin{tabular}[c]{@{}l@{}}Does not offer sufficient privacy or renders table useless for dataminers when used on multi relational database.\\ Does not guarantee diversity of sensitive attribute.\end{tabular} & Does not guarantee diversity of sensitive attribute. \\ \hline
\textbf{Strength} & Intuitive, easy to check.                                                                                                                                                                                                                                                                         &                                                                                                                                                                                                                  &                                                      \\ \hline
\end{tabular}
\end{table}
When I add \resizebox then table gets too small to read.

Code: Select all

\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\resizebox{\textwidth}{!}{\begin{tabular}{|l|l|l|l|}
\hline
\textbf{Criteria} & \textbf{k-Anonymity}                                                                                                                                                                                                                                                                              & \textbf{(X, Y)-Anonymity}                                                                                                                                                                                        & \textbf{Multirelational k-Anonymity}                 \\ \hline
\textbf{Purpose}  & Single table where each row  represents a distinct person.                                                                                                                                                                                                                                        & Single table where one person can appear in multiple rows.                                                                                                                                                       & Multiple relational tables.                          \\ \hline
\textbf{Weakness} & \begin{tabular}[c]{@{}l@{}}Does not guarantee diversity of sensitive attribute.\\ Does not guarantee sufficient privacy when person is present in multiple rows.\\ Does not offer sufficient privacy or renders table useless for dataminers when used on multi relational database.\end{tabular} & \begin{tabular}[c]{@{}l@{}}Does not offer sufficient privacy or renders table useless for dataminers when used on multi relational database.\\ Does not guarantee diversity of sensitive attribute.\end{tabular} & Does not guarantee diversity of sensitive attribute. \\ \hline
\textbf{Strength} & Intuitive, easy to check.                                                                                                                                                                                                                                                                         &                                                                                                                                                                                                                  &                                                      \\ \hline
\end{tabular}}
\end{table}
how do I get latex to automatically break lines like word does?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How do I automatically add linebreaks to my table?

Post by Johannes_B »

If i understand you correct, you want something like the following.

Code: Select all

\documentclass{article}
\usepackage{tabularx}
\usepackage{booktabs}
\renewcommand{\tabularxcolumn}[1]{>{\raggedright\arraybackslash\setlength{\parskip}{.5em}}p{#1}}
\begin{document}
\begin{table}
	\centering
	\caption{My caption}
	\label{my-label}
	\begin{tabularx}{\textwidth}{lXXX}
		\toprule
		\textbf{Criteria} & \textbf{k-Anonymity}& \textbf{(X,~Y)\-Anonymity}& \textbf{Multirelational k-Anonymity}\\ \midrule
		\textbf{Purpose}  & Single table where each row  represents a distinct person.& Single table where one person can appear in multiple rows.& Multiple relational tables.\\ \addlinespace
		\textbf{Weakness} & Does not guarantee diversity of sensitive attribute.\par Does not guarantee sufficient privacy when person is present in multiple rows.\par Does not offer sufficient privacy or renders table useless for dataminers when used on multi relational database. & Does not offer sufficient privacy or renders table useless for dataminers when used on multi relational database.\par Does not guarantee diversity of sensitive attribute. & Does not guarantee diversity of sensitive attribute. \\ \addlinespace
		\textbf{Strength} & Intuitive, easy to check.&&\\ \bottomrule
	\end{tabularx}
\end{table}
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
rain
Posts: 12
Joined: Thu Jan 28, 2016 9:00 pm

How do I automatically add linebreaks to my table?

Post by rain »

Yes, however the lines dont touch each other. They are just separate lines, but I wish to have one table. Like with tabular when all lines are connected.

Code: Select all

\begin{table}
        \centering
        \caption{My caption}
        \label{my-label}
        \begin{tabularx}{\textwidth}{|l|X|X|X|}
                \toprule
                \textbf{Criteria} & \textbf{k-Anonymity}& \textbf{(X,~Y)\-Anonymity}& \textbf{Multirelational k-Anonymity}\\ \midrule
                \textbf{Purpose}  & Single table where each row  represents a distinct person.& Single table where one person can appear in multiple rows.& Multiple relational tables.\\ \addlinespace \midrule
                \textbf{Weakness} & Does not guarantee diversity of sensitive attribute.\par Does not guarantee sufficient privacy when person is present in multiple rows.\par Does not offer sufficient privacy or renders table useless for dataminers when used on multi relational database. & Does not offer sufficient privacy or renders table useless for dataminers when used on multi relational database.\par Does not guarantee diversity of sensitive attribute. & Does not guarantee diversity of sensitive attribute. \\ \addlinespace \midrule
                \textbf{Strength} & Intuitive, easy to check.&&\\ \bottomrule
        \end{tabularx}
\end{table}
Last edited by Stefan Kottwitz on Wed Oct 19, 2016 10:20 pm, edited 1 time in total.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: How do I automatically add linebreaks to my table?

Post by Johannes_B »

Don't use the commands of package booktabs, then. It discourages vertical lines in tables as they make tables much harder to read.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
rain
Posts: 12
Joined: Thu Jan 28, 2016 9:00 pm

Re: How do I automatically add linebreaks to my table?

Post by rain »

Ok. That didnt actually help me because I dont understand how I should change this code then? What commands exactly and what would be the replacements?
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How do I automatically add linebreaks to my table?

Post by Johannes_B »

If you don't want to have your table look a bit more professional, use the commands you already knwo to look it more like a prison bar grid.

Code: Select all

\documentclass{article}
\usepackage{tabularx}
\usepackage{booktabs}
\renewcommand{\tabularxcolumn}[1]{>{\raggedright\arraybackslash\setlength{\parskip}{.5em}}p{#1}}
\begin{document}
\begin{table}
	\centering
	\caption{My caption}
	\label{my-label}
	\begin{tabularx}{\textwidth}{|l|X|X|X|}
		\hline
		\textbf{Criteria} & \textbf{k-Anonymity}& \textbf{(X,~Y)\-Anonymity}& \textbf{Multirelational k-Anonymity}\\ \hline
		\textbf{Purpose}  & Single table where each row  represents a distinct person.& Single table where one person can appear in multiple rows.& Multiple relational tables.\\ \hline
		\textbf{Weakness} & Does not guarantee diversity of sensitive attribute.\par Does not guarantee sufficient privacy when person is present in multiple rows.\par Does not offer sufficient privacy or renders table useless for dataminers when used on multi relational database. & Does not offer sufficient privacy or renders table useless for dataminers when used on multi relational database.\par Does not guarantee diversity of sensitive attribute. & Does not guarantee diversity of sensitive attribute. \\ \hline
		\textbf{Strength} & Intuitive, easy to check.&&\\ \hline
	\end{tabularx}
\end{table}
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply