Graphics, Figures & TablesVertical Lines in Table with Multiple Columns

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

Vertical Lines in Table with Multiple Columns

Post by LaTexLearner »

I'm trying to learn how to make tables containing multiple columns of the same width. Here, I tried to make one table with 8 columns each of width 1.5cm. The borders didn't work out, though. The problem, I'm pretty sure, has to do with the placement of the "|" character.

THIS GAVE ME TWO VERTICAL LINES IN SOME PLACES

Code: Select all

\documentclass[12pt]{article}
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}

\begin{document}

\begin{tabular}{*{8}{|p{1.5cm}|}} \hline
1&2&3&4&5&6&7&8\\ \hline
1&2&3&4&5&6&7&8\\ \hline
\end{tabular}

\end{document}
THIS GAVE ME AN ERROR MESSAGE... so does that mean the "|" things have to go inside the {p{1.5cm}} part?

Code: Select all

\documentclass[12pt]{article}
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}

\begin{document}

\begin{tabular}{*{8}|{p{1.5cm}}|} \hline
1&2&3&4&5&6&7&8\\ \hline
1&2&3&4&5&6&7&8\\ \hline
\end{tabular}

\end{document}
Where in the code should I have put the "|" thing and why?
Last edited by LaTexLearner on Wed Jul 22, 2015 2:53 am, edited 1 time in total.

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

Re: Vertical Lines in Table with Multiple Columns

Post by Johannes_B »

I am a bit tired right now, or in other words: lazy.


Having a compilable example is always good to test right away, it also ensures, that we both see the same. Please complete your example, and i'll have a look at it tomorrow ;-)
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

Vertical Lines in Table with Multiple Columns

Post by LaTexLearner »

Johannes_B wrote:I am a bit tired right now, or in other words: lazy.


Having a compilable example is always good to test right away, it also ensures, that we both see the same. Please complete your example, and i'll have a look at it tomorrow ;-)
I thought the code I gave originally was enough, since the first one actually did work in the "open in write latex" link... but I've edited both sets of of code so that they are now documents in an of themselves.

When you're re-energized, I look forward to your response. :)
mas
Posts: 226
Joined: Thu Dec 04, 2008 4:39 am

Vertical Lines in Table with Multiple Columns

Post by mas »

Next time, post the error message also :-)

The problem as the error message suggested is in the placement of the | character. The code below works:

Code: Select all

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{tabular}{*{8}{|p{1.5cm}|}} \hline
1&2&3&4&5&6&7&8\\ \hline
1&2&3&4&5&6&7&8\\ \hline
\end{tabular}

\end{document}
A suggestion: Avoid vertical rules in a table unless it is absolutely necessary for a purpose. Take a look at the booktabs documentation.

OS: Debian/GNU Linux; LaTeX System : TeXLive; Editor : Vim
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Vertical Lines in Table with Multiple Columns

Post by LaTexLearner »

mas wrote:Next time, post the error message also :-)

The problem as the error message suggested is in the placement of the | character. The code below works:

Code: Select all

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{tabular}{*{8}{|p{1.5cm}|}} \hline
1&2&3&4&5&6&7&8\\ \hline
1&2&3&4&5&6&7&8\\ \hline
\end{tabular}

\end{document}
I just tried that code in the "write latex" link and it too created the double vertical lines in some places. Why did that happen and how do you fix that?
mas wrote: A suggestion: Avoid vertical rules in a table unless it is absolutely necessary for a purpose. Take a look at the booktabs documentation.
They will be quite necessary for what I'm doing, i.e. worksheets for students aged 8+ that need to know exactly where to write answers.
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Vertical Lines in Table with Multiple Columns

Post by rais »

LaTexLearner wrote: I just tried that code in the "write latex" link and it too created the double vertical lines in some places. Why did that happen and how do you fix that?
A column specification of {*{8}{|p{1.5cm}|}} means: create eight columns, each surrounded by vertical rules. That results in rules between two columns being doubled: the right rule of the column left of the double rule and the left rule of the column right of the double rule build it.
So, don't repeat both rules
{|*{8}{p{1.5cm}|}} would repeat only the column with its right rule, or
{*{8}{|p{1.5cm}}|} would repeat the column with its left rule.

KR
Rainer
Post Reply