Graphics, Figures & TablesTable Question #2: Why the right-most column?

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 #2: Why the right-most column?

Post by LaTexLearner »

Why are all the equations in the table going into the right-most column? I'd like them to be in the left-most column. When I try to add new content using the "&" symbol in those rows - which I thought would make it more obvious that I wanted the equations to be in the left-most column - it gives me errors.

What's going on?

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$\\  %typing "& empty & empty & empty" before the line break gives me an error, "Extra alignment tab..."

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

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

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

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

\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: 10330
Joined: Mon Mar 10, 2008 9:44 pm

Table Question #2: Why the right-most column?

Post by Stefan Kottwitz »

\raggedright changes the line break macro \\, you can fix it by adding \arraybackslash afterwards:

Code: Select all

\raggedright\arraybackslash Solution to The System?
Btw. you can define a column type for left aligned columns:

Code: Select all

\newcolumntype{L}[1]{>{\raggedright\arraybackslash\hspace{0pt}}p{#1}}
...
\begin{tabular}{|L{5cm}|*3{L{2.5cm}|}}
In that case you can remove the \raggedright within the cells.

I inserted \hspace{0pt} because TeX doesn't break the first word in a paragraph box, as a fix.

Btw. better use \[ ... \] (LaTeX, higher level) instead of $$...$$ (TeX, lower level) because the latter gives inconsistent vertical distance if used in LaTeX instead of TeX.

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

Table Question #2: Why the right-most column?

Post by LaTexLearner »

Stefan_K wrote:\raggedright changes the line break macro \\, you can fix it by adding \arraybackslash afterwards:

Code: Select all

\raggedright\arraybackslash Solution to The System?
I've updated the code with this and it looks better now. Thanks!

I still don't quite understand how it all worked though. How did resetting the line break macro place all the contents to the right-most column?
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Table Question #2: Why the right-most column?

Post by LaTexLearner »

Stefan_K wrote:Btw. you can define a column type for left aligned columns:

Code: Select all

\newcolumntype{L}[1]{>{\raggedright\arraybackslash\hspace{0pt}}p{#1}}
...
\begin{tabular}{|L{5cm}|*3{L{2.5cm}|}}
In that case you can remove the \raggedright within the cells.

I inserted \hspace{0pt} because TeX doesn't break the first word in a paragraph box, as a fix.
Cool. It makes way more sense to define a new column type so I don't have to keep typing the same thing over and over again. I'll be making LOTS of worksheets and those will need LOTS of tables, so this sounds like something I should master.

I understand the *purpose* of the code, but I can't make sense of the code itself. This is where I'm at:
- The {L} just names the new column.
- What does the [1] and the first ">"do?
- What is the p{#1} for?
- The "*3" part means three columns with the same properties, right?
Stefan_K wrote: Btw. better use \[ ... \] (LaTeX, higher level) instead of $$...$$ (TeX, lower level) because the latter gives inconsistent vertical distance if used in LaTeX instead of TeX.

Stefan
What did you mean by higher and lower level? And is consistent vertical spacing the only advantage of this higher level?

PS I just tried changing $...$ to \[...\]. Will this always centre the math stuff? And also, why is there a backslash before the final square bracket?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10330
Joined: Mon Mar 10, 2008 9:44 pm

Table Question #2: Why the right-most column?

Post by Stefan Kottwitz »

LaTexLearner wrote:Cool. It makes way more sense to define a new column type so I don't have to keep typing the same thing over and over again.
Yes. And it's more than saving typing! Because once you defined column types (and macros in general too) you can easily change things globally. For example, you like to change column alignment, or width because of page settings or font change, you can do it at this column definition place instead of all places in the document manually. That's the general advantage of macros.
LaTexLearner wrote:I understand the *purpose* of the code, but I can't make sense of the code itself. This is where I'm at:
- The {L} just names the new column.
- What does the [1] and the first ">"do?
- What is the p{#1} for?
- The "*3" part means three columns with the same properties, right?
Yes, L is the new column type. [1] means we got 1 argument, which is the column width in our case. p{#1} is a paragraph-column (that means it can break lines) with a certain width. #1 is the code for the argument 1, which is the width. We made L similar to the standard column p. I suggest look for \newcommand in a LaTeX tutorial, it's worth learning this command.
LaTexLearner wrote:What did you mean by higher and lower level? And is consistent vertical spacing the only advantage of this higher level?
I mean, TeX is the basis and it's fine and correct. LaTeX builds on it with a lot of macros. They are designed to work well together. Whenever there's a LaTeX command and a TeX command, it's preferable to work with a LaTeX command (in LaTeX) because it's usually extended and consistent with all other LaTeX commands. TeX commands don't know about LaTeX commands. So, if LaTeX designs spacing between math environments, the TeX command doesn't know it. The LaTeX commands is designed with knowing other LaTeX commands. This is a general recommendation: on higher abstraction level, better stay at that level and only step to lower level (TeX, Assembler, machine code ;-) ) if really needed.
LaTexLearner wrote:PS I just tried changing $...$ to \[...\]. Will this always centre the math stuff?
For TeX's $ ... $ the LaTeX code is \( ... \). Note the parentheses. This is inline math, that is, math formulas within normal text.
For TeX's $$ ... $$ the LaTeX code is \[ ... \]. Here we have square brackets. This is displayed math, that is, math formulas in their own paragraph, with space before and after it, and centered by default.

Actually those are shortcuts for \begin{displaymath} ... \end{displaymath} etc.
LaTexLearner wrote:And also, why is there a backslash before the final square bracket?
That's because they are commands, and commands begin with a backslash. We have exceptions, to reduce typing, such as the $ sign, which is a so called active character. It has the meaning of a command.

Stefan
LaTeX.org admin
Post Reply