Graphics, Figures & TablesTable Question #1: Border Problems

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

Table Question #1: Border Problems

Post by LaTexLearner »

Why don't the borders of my table touch? And why does it give me errors when I try to put \midrule or \bottomrule anywhere in between or below the equations?

Code: Select all


\documentclass[12pt,letterpaper]{article}

\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{array}
\usepackage{booktabs}

\title{What is a ``Solution" to a System of Equations?}
\date{}
\author{\LaTeX\ Learner}

\begin{document}

\maketitle

\noindent \textbf{The System}\\

\vspace{5pt}

\noindent Equation 1: $5x+3y=9$\\

\vspace{5pt}

\noindent Equation 2: $-4y+10=x+15$

\vspace{5pt}

\begin{tabular}{|p{5cm}|p{2.5cm}|p{2.5cm}|p{2.5cm}|}
\midrule
Possible Solution&\raggedright Solution to Equation 1?&\raggedright Solution to Equation 2?& \raggedright Solution to The System? \\
$x=2, y=5$\\

$$x=1, y=2$$\\

$$x=3, y=-2$$\\

$$(-2,3)$$\\

\midrule  %this gives me an error

$$(-5,0)$$\\

\bottomrule  %this gives me an error

\end{tabular} 

\end{document}

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

Stefan Kottwitz
Site Admin
Posts: 10334
Joined: Mon Mar 10, 2008 9:44 pm

Table Question #1: Border Problems

Post by Stefan Kottwitz »

booktabs adds vertical space which affects the vertical rules. However, vertical rules are considered to be bad anyway, so booktabs doesn't care about that effect.

You could define your own commands, also using book tabs, such as by:

Code: Select all

\newcommand{\mytoprule}{\specialrule{2pt}{0em}{0em}}
\newcommand{\mymidrule}{\specialrule{\arrayrulewidth}{0em}{0em}}
\newcommand{\mybottomrule}{\specialrule{2pt}{0em}{0em}} 
and use these instead.

Stefan
LaTeX.org admin
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Table Question #1: Border Problems

Post by LaTexLearner »

Stefan_K wrote:...However, vertical rules are considered to be bad anyway, so booktabs doesn't care about that effect...
Why are they considered bad in general?

For my use they are surely justified as I'm trying to make math exercises and worksheets that make it easier for high school students to track and display their work. Such students will range from severely disabled to years above grade-level. They need the vertical lines to keep their work neat.
Stefan_K wrote:... You could define your own commands, also using book tabs, such as by:

Code: Select all

\newcommand{\mytoprule}{\specialrule{2pt}{0em}{0em}}
\newcommand{\mymidrule}{\specialrule{\arrayrulewidth}{0em}{0em}}
\newcommand{\mybottomrule}{\specialrule{2pt}{0em}{0em}} 
and use these instead.
Ahh, custom commands again... It's going to take me a while to master those.

When you wrote \newcommand{\mytoprule}{\specialrule{2pt}{0em}{0em}}

What does {0em}{0em} do?

And what does the \arrayrulewidth do but only for the midrule?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10334
Joined: Mon Mar 10, 2008 9:44 pm

Table Question #1: Border Problems

Post by Stefan Kottwitz »

LaTexLearner wrote:
Stefan_K wrote:...However, vertical rules are considered to be bad anyway, so booktabs doesn't care about that effect...
Why are they considered bad in general?
Because we usually read row by row, and crossing vertical lines makes reading harder. Here's just a quick table example from my book:
table.png
table.png (33.4 KiB) Viewed 6362 times
The code is here in my blog. Now add vertical lines for each column. Would it become better readable?

I would only accept vertical lines if it's really required for separating and for leading the eye in a vertical way. Otherwise it's just too much. Whitespace between columns is a more decent separator.

Stefan
LaTeX.org admin
User avatar
Stefan Kottwitz
Site Admin
Posts: 10334
Joined: Mon Mar 10, 2008 9:44 pm

Table Question #1: Border Problems

Post by Stefan Kottwitz »

LaTexLearner wrote:Ahh, custom commands again... It's going to take me a while to master those.
Believe me, it's really useful! You can redefine anything. But also important, you can define macros for your own things, to get consistent markup.
LaTexLearner wrote:When you wrote \newcommand{\mytoprule}{\specialrule{2pt}{0em}{0em} What does {0em}{0em} do?
It is the white space / distance above and below the line. A midrule would add whitespace, that's why the gap in the vertical lines. So I defined a rule with zero space before and after.

You still can get whitespace by \renewcommand*{\arraystretch}{1.4} or another value.
LaTexLearner wrote:And what does the \arrayrulewidth do but only for the midrule?
I took it because it's the default line thickness for tables. The others I made thicker to a specific value, you can choose anything there.

Stefan
LaTeX.org admin
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Table Question #1: Border Problems

Post by LaTexLearner »

Stefan_K wrote:
LaTexLearner wrote:When you wrote \newcommand{\mytoprule}{\specialrule{2pt}{0em}{0em} What does {0em}{0em} do?
It is the white space / distance above and below the line. A midrule would add whitespace, that's why the gap in the vertical lines. So I defined a rule with zero space before and after.

...
It seems this custom command only works if the booktabs package is there. Is there an easier way to customize table borders? Or should I just get used to using lots of packages, even for short document?
Stefan_K wrote:...
LaTexLearner wrote:And what does the \arrayrulewidth do but only for the midrule?
I took it because it's the default line thickness for tables. The others I made thicker to a specific value, you can choose anything there.
What's the default thickness for tables? 0.5pt?
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Table Question #1: Border Problems

Post by LaTexLearner »

When I wrote

Code: Select all

\begin{tabular}{|L{4cm}|*3{L{2.5cm}|}}
I expected the vertical lines to show for all the columns, from the top of the table to the bottom. What happened?

Also, how much of this code could I have left out to still have a
"minimum working example"?

Code: Select all

\documentclass[12pt,letterpaper]{article}

\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{array}
\usepackage{booktabs}

\newcolumntype{L}[1]{>{\raggedright\arraybackslash\hspace{0pt}}p{#1}}

\newcommand{\myrule}{\specialrule{0.4pt}{0em}{0em}}


\title{What is a ``Solution" to a System of Equations?}
\date{}
\author{\LaTeX\ Learner}

\begin{document}

\maketitle

\noindent \textbf{The System}\\

\vspace{5pt}

\noindent Equation 1: \(5x+3y=9\)\\

\vspace{5pt}

\noindent Equation 2: \(-4y+10=x+15\)\\

\vspace{5pt}

\begin{tabular}{|L{4cm}|*3{L{2.5cm}|}}
\myrule
Possible Solution & Solution to Equation 1? & Solution to Equation 2? & Solution to The System? \\
\myrule

\(x=2, y=5\)\\

\myrule

\(x=1, y=2\)\\

\myrule

\(x=3, y=-2\)\\

\myrule

\((-2,3)\)\\

\myrule

\((-5,0)\)\\

\myrule

\end{tabular} 

\end{document}

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

Table Question #1: Border Problems

Post by Johannes_B »

Packages are a blessing and a curse. You need one for font encoding, one for input encoding. One for tables (array), one for the nice rules (booktabs), including specialrule, once for this and one for that.

Or you copy the bits and pieces from the package files, which is quite a bad idea. You would have to check licence issues and can't keep track of updates.

The standard rule width for arrayrules is 0.4pt. You can find that out using \showthe\arrayrulewidth, the result will be written into the log file. Compiling interactively, LaTeX will halt, you can check and continue by pressing enter. Texworks runs in interactive mode by default.


Concerning the lines in the table, you have given only one field. LaTeX now thinks, that this line of the table is complete and does not bother with the stuff that is not there. Simply fill up every line with the needed number of ampersands.

Please notice in the code below the \noindent. Can you figure out what it does?

Code: Select all

\documentclass[12pt,letterpaper]{article}

\usepackage{array}
\usepackage{booktabs}

\newcolumntype{L}[1]{>{\raggedright\arraybackslash\hspace{0pt}}p{#1}}

\newcommand{\myrule}{\specialrule{0.4pt}{0em}{0em}}


\title{What is a ``Solution" to a System of Equations?}
\date{}
\author{\LaTeX\ Learner}

\begin{document}

\maketitle

\noindent \textbf{The System}\\

\vspace{5pt}

\noindent Equation 1: \(5x+3y=9\)\\

\vspace{5pt}

\noindent Equation 2: \(-4y+10=x+15\)\\

\showthe\arrayrulewidth
\vspace{5pt}

\noindent%<-Notice this? 
\begin{tabular}{|L{4cm}|*3{L{2.5cm}|}}
	\myrule
	Possible Solution & Solution to Equation 1? & Solution to Equation 2? & Solution to The System? \\
	\myrule

	\(x=2, y=5\)\\

	\myrule

	\(x=1, y=2\) & & & \\

	\myrule

	\(x=3, y=-2\)\\

	\myrule

	\((-2,3)\)\\

	\myrule

	\((-5,0)\) & & & \\

	\myrule

\end{tabular}

\end{document}
Edit: Oh, you used \noindent yourself. Btw, all that spaces of 5pt ... I think defining a macro \answerspacing or something similar would be a good idea. As mentioned before, you would have the ability to change the spacing globally.
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 Question #1: Border Problems

Post by LaTexLearner »

Johannes_B wrote:Packages are a blessing and a curse. You need one for font encoding, one for input encoding. One for tables (array), one for the nice rules (booktabs), including specialrule, once for this and one for that.

Or you copy the bits and pieces from the package files, which is quite a bad idea. You would have to check licence issues and can't keep track of updates.
Well, I definitely won't be doing any copying of bits and pieces from packages, then! lol

It sounds like there is no way around large numbers of packages, even for the document I've been discussing here which will be all of three pages by the time it's done.

Where does one go about learning about "basic" or the best or the most common packages to use? I just went to the https://www.ctan.org/pkg/ but it doesn't appear to really describe the packages or rank them in a way that's useful for a newb.

Johannes_B wrote:Concerning the lines in the table, you have given only one field. LaTeX now thinks, that this line of the table is complete and does not bother with the stuff that is not there. Simply fill up every line with the needed number of ampersands.
Issue resolved. Thank you!
Johannes_B wrote: Please notice in the code below the \noindent. Can you figure out what it does?

Code: Select all

\documentclass[12pt,letterpaper]{article}

\usepackage{array}
\usepackage{booktabs}

\newcolumntype{L}[1]{>{\raggedright\arraybackslash\hspace{0pt}}p{#1}}

\newcommand{\myrule}{\specialrule{0.4pt}{0em}{0em}}


\title{What is a ``Solution" to a System of Equations?}
\date{}
\author{\LaTeX\ Learner}

\begin{document}

\maketitle

\noindent \textbf{The System}\\

\vspace{5pt}

\noindent Equation 1: \(5x+3y=9\)\\

\vspace{5pt}

\noindent Equation 2: \(-4y+10=x+15\)\\

\showthe\arrayrulewidth
\vspace{5pt}

\noindent%<-Notice this? 
\begin{tabular}{|L{4cm}|*3{L{2.5cm}|}}
	\myrule
	Possible Solution & Solution to Equation 1? & Solution to Equation 2? & Solution to The System? \\
	\myrule

	\(x=2, y=5\)\\

	\myrule

	\(x=1, y=2\) & & & \\

	\myrule

	\(x=3, y=-2\)\\

	\myrule

	\((-2,3)\)\\

	\myrule

	\((-5,0)\) & & & \\

	\myrule

\end{tabular}

\end{document}
Edit: Oh, you used \noindent yourself. Btw, all that spaces of 5pt ... I think defining a macro \answerspacing or something similar would be a good idea. As mentioned before, you would have the ability to change the spacing globally.
I think that the \noindent command you pointed out just moved the tables all the way over to the left. I'm not sure why it began as indented before I typed that.

And... yes, I do need to figure out macros, custom commands, custom environments and all that. I have my work cut out for me!
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: Table Question #1: Border Problems

Post by Johannes_B »

How to find out which package is good or recommended? This is a very very hard task for a beginner. An up-to-date introduction will show you the basic packages.

For the most specialized stuff, a google search can be helpful, but you need to check for the date. Some solution found on a newsgroup in 1998 might, or might not be a good solution today. It is very hard to impossible to assess the quality.

You can always ask a question though. Most LaTeX helpers try to be up-to-date, check development, test, talk etc.
On the other hand, some helpers just post a solution they found in 1998 when they began. Same problem as mentioned above.

One thing that will push your knowledge once you know the basics is following the forum threads. Traffic is quite low at the moment, so you can follow most of the stuff going on here if you save half an hour per day. It will be worth it ;-)
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