Wrapping multiple words concatenated with / symbols in latex tables - latex

I'm trying to make a latex table, and I want to insert a string of the form word/anotherword/athirdword/afourthword/andsoon. This doesn't work though, as it will overflow instead of wrapping to the next line. If I just remove all the "/" characters, it works well. Below are some examples:
Correctly working word wrap without "/":
\begin{center}
\begin{tabular}{ |p{10em}|p{10em}|p{10em}| }
\hline
a wordanotherwordathirdwordafourthwordandsoon &
Next column &
Third column \\
\hline
\end{tabular}
\end{center}
Overflowing with "/":
\begin{center}
\begin{tabular}{ |p{10em}|p{10em}|p{10em}| }
\hline
a word/anotherword/athirdword/afourthword/andsoon &
Next column &
Third column \\
\hline
\end{tabular}
\end{center}
Another oddity, if I remove the first word "a" from the cell, the word overflows no matter what.
I tried to search SO for help, but the closest I was this SO post, which doesn't really address my problem.
EDIT: Here is the minimal reprex I mention in the comment:
\documentclass{article}
\usepackage[utf8]{inputenc}
\title{Title}
\author{}
\date{}
\begin{document}
\maketitle
\begin{center}
\begin{tabular}{ |p{10em}|p{10em}|p{10em}| }
\hline
a wordanotherwordathirdwordafourthwordandsoon &
Next column &
Third column \\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{ |p{10em}|p{10em}|p{10em}| }
\hline
a word/anotherword/athirdword/afourthword/andsoon &
Next column &
Third column \\
\hline
\end{tabular}
\end{center}
\end{document}

Related

LaTex table - Column size and text alignment

I am quite a beginner in LaTex and generated the following table:
\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{|p{2,5cm}||p{3,5cm}|p{2cm}| p{2cm}|}
\hline
\multicolumn{4}{|c|}{Initial Run of Models} \\
\hline
Objective & Service Level in \% & Costs in € & Number of Lines \\
\hline
Max. direct pass. & 93.48 & 1,258.41 & 7\\
Min. travel time & 77.99 & 933.13 & 7\\
\hline
\end{tabular}
\caption[Initial Run of Models]{\label{Tab:InitialRun}Initial Run of Models}
\end{table}
\end{document}
Is there any way to maintain the width of the columns and still align the text in the columns to be on the right side?
You could use the array package and add >{\raggedleft}:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{array}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{|>{\raggedleft}p{2.5cm}||>{\raggedleft}p{3.5cm}|>{\raggedleft}p{2cm}| >{\raggedleft\arraybackslash}p{2cm}|}
\hline
\multicolumn{4}{|c|}{Initial Run of Models} \\
\hline
Objective & Service Level in \% & Costs in € & Number of Lines \\
\hline
Max. direct pass. & 93.48 & 1,258.41 & 7\\
Min. travel time & 77.99 & 933.13 & 7\\
\hline
\end{tabular}
\caption[Initial Run of Models]{\label{Tab:InitialRun}Initial Run of Models}
\end{table}
\end{document}
Personally, I would suggest to use the tabularray package instead. This package makes it easy to set the alignment for row/columns or individual cells.
You could use Q[2.5cm] to specify a width, but if you don't, tabularray will determine a much better width for you.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularray}
\begin{document}
\begin{table}[h]
\centering
\begin{tblr}{
colspec={|Q||Q|Q|Q|},
rows={halign=r},
column{1}={halign=l},
row{1}={halign=c},
row{2}={halign=l},
}
\hline
\SetCell[c=4]{} Initial Run of Models &&& \\
\hline
Objective & Service Level in \% & Costs in € & Number of Lines \\
\hline
Max. direct pass. & 93.48 & 1,258.41 & 7\\
Min. travel time & 77.99 & 933.13 & 7\\
\hline
\end{tblr}
\caption[Initial Run of Models]{\label{Tab:InitialRun}Initial Run of Models}
\end{table}
\end{document}
If you use the package array as suggested in the first answer, then you can define in your header one or more newcolumntypes, for example this one called R, right aligned with the width to be assigned to each column. More details here.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{array}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{|R{2,5cm}||R{3,5cm}|R{2cm}|R{2cm}|}
\hline
\multicolumn{4}{|c|}{Initial Run of Models}\\
\hline
Objective & Service Level in \% & Costs in € & Number of Lines\\
\hline
Max. direct pass. & 93.48 & 1,258.41 & 7\\
Min. travel time & 77.99 & 933.13 & 7\\
\hline
\end{tabular}
\caption{\label{Tab:InitialRun}Initial Run of Models}
\end{table}
\end{document}
Here I changed to R all of the four columns but they don't have to be all of the same kind.
Make a search because surely there are many "duplicates" of this question here and on TeX SE.

An alternative to write multirow in latex's tabular?

In latex, I know we can use the multirow command like the following,
\begin{table}[!h]
\centering
\begin{tabular}{|c|l|}
\hline
\multirow{2}{*}{A}
& I want to place this sentence in multiple lines, \\
& but don't want to control the linebreak myself \\
\hline
\end{tabular}
\end{table}
I think it is so stupid to control the linebreak myself.
Any other alternative that fits the text width to line width?
\documentclass{article}
\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}}
\begin{document}
\begin{table}[htbp]
\centering
\begin{tabularx}{\linewidth}{|c|X|}
\hline
A
& I want to place this sentence in multiple lines, but don't want to control the linebreak myself \\
\hline
\end{tabularx}
\end{table}
\end{document}

\footnote gives error in minipage environment with cta-author class

I need to use \footnote in a minipage environment, but it always gives an error
Command \itshape invalid in math mode. a\footnote{
mwe is
\documentclass{cta-author}
\begin{document}
\begin{table}
\begin{minipage}{\columnwidth}
\begin{tabular}{|c|c|}
\hline
Column 1 & Column2 \\
a\footnote{footnote example} & b\\
\hline
\end{tabular}
\end{minipage}
\end{table}
\end{document}
Latex class file is here
You can redefine the way cta-author makes the footnote mark. Instead of $^{...}$, use \textsuperscript{...}:
\documentclass{cta-author}
\makeatletter
\def\#makefnmark{\textsuperscript{\#thefnmark}}
\makeatother
\begin{document}
\begin{table}
\begin{minipage}{\columnwidth}
\begin{tabular}{|c|c|}
\hline
Column 1 & Column2 \\
a\footnote{footnote example} & b \\
\hline
\end{tabular}
\end{minipage}
\end{table}
% A possible work-around
\begin{table}
\begin{tabular}{|c|c|}
\hline
Column 1 & Column2 \\
a$^*$ & b \\
\hline
\end{tabular}
\medskip
\footnoterule
{\itshape\footnotesize $^*$footnote example}
\end{table}
\end{document}
And here is a visual of the work-around:

rowcolors fill whole row and not just the table row

Ok so i wrote this simple code (shown below) to create an alternate colour table using \rowcolors, yet instead of just colouring the rows of the table it colours the whole row (even more than textwidth). Any help how to fix this?
begin{table}[ht]
\scriptsize
\begin{center}
\rowcolors{1}{lightgray}{white}
\caption{...}
\begin{tabular}{p{0.45\textwidth} | p{0.55\textwidth}}
Filename & Contents \\
\hline
\hline
A & B \\
C & F \\
\end{tabular}
\end{center}
\end{table}
While 0.45\textwidth + 0.55\textwidth would seem to fit within \textwidth, each of your p-columns have an extra column separation... on both sides. To that end, you should remove them in order to make it fit within the text block boundary:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{lipsum}
\begin{document}
\begin{table}[ht]
\scriptsize\centering
\rowcolors{1}{lightgray}{white}
\caption{This is a table.}
\begin{tabular}{
p{\dimexpr0.45\textwidth-2\tabcolsep} |
p{\dimexpr0.55\textwidth-2\tabcolsep}}
Filename & Contents \\
\hline
\hline
A & B \\
C & F
\end{tabular}
\end{table}
\lipsum[1]
\end{document}
Also, don't use the center environment; use \centering instead.

How to create a new line in the table cell

I am new to latex, and I am trying to create a table, but the content in the table cell is quite long, so I decided to create a new line for the cells.
But I am not sure how to do that, could someone help me on that? Here is my code:
\begin{table}[h!]
\caption{Multirow table}
\begin{center}
\begin{tabular}{ |c|c|c| }
\hline
Observation(Species name) & Likelihood (Limnodynastes peronii Distribution Model) &Likelihood (Rhinella marina Distribution Model)\\
\hline
Observation 1 (Limnodynastes peronii) &0.0712 &0.2699\\
\hline
Observation 2 (Rhinella marina) &0.30 &0.013 \\
\hline
\end{tabular}
\end{center}
\end{table}
Here is a fix I made up. The command wraps information in a parbox with a little extra spacing above and below the text to make it look nice. This allows you to limit the width of a cell as well as enter manual line breaks.
\begin{table}[h!]
\caption{Multirow table}
\newcommand{\wrap}[1]{\parbox{.33\linewidth}{\vspace{1.5mm}#1\vspace{1mm}}}
\begin{center}
\begin{tabular}{ |c|c|c| }
\hline
\wrap{Observation\\ (Species name)} &\wrap{Likelihood\\ (Limnodynastes peronii Distribution Model)} &\wrap{Likelihood\\ (Rhinella marina\\ Distribution Model)}\\
\hline
\wrap{Observation 1\\ (Limnodynastes peronii)} &0.0712 &0.2699\\
\hline
\wrap{Observation 2\\ (Rhinella marina)}&0.30 &0.013 \\
\hline
\end{tabular}
\end{center}
\end{table}
Hope this helps- this is my first answer on stackoverflow!

Resources