Graphics, Figures & TablesTable Border in Weird Place

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Table Border in Weird Place

Post by LaTexLearner »

I'm trying to learn LaTeX tables in order to make properly laid-out worksheets.

In my most recent experiment, the right-most border was, for some reason, placed far away from the rest of the table. What happened?

Code: Select all

\begin{tabular*}{11cm}{|p{4cm}|p{7cm}|} \hline
4cm & 7cm \\ \hline
\end{tabular*}

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

Table Border in Weird Place

Post by Johannes_B »

I am tired, guess you read it in the previous question.

I am not sure, but i think your current setup will give you an overfull box. 7 and 4 add up to 11 perfectly, but the lines have a width as well, and every column gets a bit of white space at both sides. That will make the contents a bit wider than 11 cm.

By the way, do you know package tabularx? There are a few similar ones, but ... tired.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Table Border in Weird Place

Post by LaTexLearner »

Johannes_B wrote:I am tired, guess you read it in the previous question.

I am not sure, but i think your current setup will give you an overfull box. 7 and 4 add up to 11 perfectly, but the lines have a width as well, and every column gets a bit of white space at both sides. That will make the contents a bit wider than 11 cm.

By the way, do you know package tabularx? There are a few similar ones, but ... tired.
I see.

I just started using tabularx. It seems pretty easy to use for making column widths work with table width. Thanks!
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Table Border in Weird Place

Post by Johannes_B »

I mentioned the white space on each side of column text; please have a look:

Code: Select all

\documentclass{article}
\usepackage{calc}
\begin{document}
\begin{tabular*}{11cm}{|p{4cm-2\tabcolsep}|p{7cm-2\tabcolsep}|} \hline
	4cm & 7cm \\ \hline
\end{tabular*}
\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.
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Table Border in Weird Place

Post by LaTexLearner »

I can see that the output of the code is what I was looking for, but I don't quite get how the code created that.

Code: Select all

\begin{tabular*}{11cm}{|p{4cm-2\tabcolsep}|p{7cm-2\tabcolsep}|} \hline
Questions about this code:
  • What does the * do in {tabular*}?
  • What is the normal amount of white space on either side of the content of cells in tables?
  • Does -2\tabcolsep eliminate that space?
  • How do I fix this list? lol
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Table Border in Weird Place

Post by Johannes_B »

The tabular* environment takes an extra argument, the width the tabular should take; in your case 11cm. A relative width might be better tough, for example .8\linewidth.

You can print stuff like the current value of a length or a counter using \the, in our example it would be \the\tabcolsep.

The space is not eliminated. In order to have a table of 11 cm, we need to substract the sep two times (one on each side). You can remove that space completely, but look how ugly that is:

Code: Select all

\documentclass{article}
\usepackage{calc}
\usepackage{blindtext}
\begin{document}
\the\tabcolsep

\begin{tabular*}{11cm}{|p{4cm-2\tabcolsep}|p{7cm-2\tabcolsep}|} \hline
	4cm minus 6 pt on each side & 7cm minus 12 pt \blindtext \\ \hline
\end{tabular*}

\setlength{\tabcolsep}{0pt}
\begin{tabular*}{11cm}{|p{4cm-2\tabcolsep}|p{7cm-2\tabcolsep}|} \hline
	4cm of printable text \blindtext & 7cm \blindtext \\ \hline
\end{tabular*}
\end{document}
http://www.bbcode.org/examples/?id=12 or "edit" your last post and look how i fixed it.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Table Border in Weird Place

Post by rais »

I don't see an advantage in using the tabular* environment here at all.
If the sum of all column widths plus two \tabcolsep per column exceeds the specified width, the tabular's body will stick out to the right or each \hline will be too short;
If the sum of all column widths plus two \tabcolsep per column is shorter than the specified width, each \hline will be too long.
So unless you put expandable space into your column specification, which could be done by @{\extracolsep{\fill}} within the column specification, and have some space to fill (that is, the sum of all column widths plus two \tabcolsep per column is indeed shorter than the specified width), the usage of tabular* does not bring any advantage compared to {tabular}; even then it looks a bit awkward with vertical rules inside.
Besides, it's easier and shorter to write

Code: Select all

\begin{tabular}{<column specification>}
    <table body>
\end{tabular}
anyway.

Strange, what happened to [env]tabular*[/env]? Does it not work with starred variants?

KR
Rainer
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Table Border in Weird Place

Post by Johannes_B »

rais wrote:Strange, what happened to [env]tabular*[/env]? Does it not work with starred variants?

The env and cmd tags link to our own reference manual. There is a dedicated entry for tabular in which the starred variant is mentioned as well.

Package tabularx might be better here. But since the question arose what the starred variant is/does, usual tabular might be best. But the discussion was very theoretical and it is hard to suggest anything more suited.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Table Border in Weird Place

Post by rais »

Johannes_B wrote:
rais wrote:Strange, what happened to [env]tabular*[/env]? Does it not work with starred variants?

The env and cmd tags link to our own reference manual. There is a dedicated entry for tabular in which the starred variant is mentioned as well.
Thank you, I see.
Shouldn't the starred variant be linked to the same entry, then?
What got me really baffled was [env]...[/env] resulted in {...}---but I'm getting off topic (happens sometimes when I don't heed your excellent advice about calming down, breathing... ;)
Johannes_B wrote: Package tabularx might be better here. But since the question arose what the starred variant is/does, usual tabular might be best. But the discussion was very theoretical and it is hard to suggest anything more suited.
Full ACK.

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

Re: Table Border in Weird Place

Post by Johannes_B »

I am not completely sure how it works, i bet it is a simple expansion into a link. I myselve never use those.

Somehow i don't like the fact, that the very same information is cluttered all over the net on its own. As time goes by, things get obsolete and change, and not all of the sites will update the information.
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