Graphics, Figures & Tableslongtable / table column alignment

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
NChairFix
Posts: 7
Joined: Thu May 21, 2015 9:08 am

longtable / table column alignment

Post by NChairFix »

I have been working on some manuals that display table in a longtable environment using a counter on a line.
A command has been created to fill in a row in the table for ease of use, this command has an optional argument and it would appear that that particular argument messes us the centering of the first column.
I have created a MWE below:

Code: Select all

\documentclass[]{report}

\usepackage{array}
\usepackage{longtable}

% create my new counter
\newcounter{mycounter}
\setcounter{mycounter}{0}

% #1 inc.count
% #2 table column
\newcommand{\doline}[2]
{
	 \arabic{mycounter} & #2 & ????	\addtocounter{mycounter}{#1} \\
}

% #1 = inc. count
% #2 = table column
\newcommand{\tablelineWORKS}[2]
{
	\doline{#1}{#2}
}

% #1 = optional argument
% #2 = inc. count
% #3 = table column
\newcommand{\tablelineNOTWORKS}[3][empty]
{
	\doline{#2}{#3}
}

\begin{document}
	This long table doesn't work:	
	
	\begin{longtable}{>{\centering\arraybackslash}m{1.5cm}>{\centering\arraybackslash}m{6cm}m{2cm}} 
	\hline
	HEADER 1 & HEADER 2 & HEADER 3 \\
	\hline
	\tablelineWORKS{2}{this line works!}
	
	\tablelineNOTWORKS{2}{BAD LINE! NO optional param}
	\tablelineNOTWORKS[xxxx]{2}{BAD LINE! WITH optional param}

	\tablelineWORKS{2}{this line works!}
	\hline
	\end{longtable}


	This table is working:		
	
	\begin{tabular}{>{\centering\arraybackslash}m{1.5cm}>{\centering\arraybackslash}m{6cm}m{2cm}} 
	\hline
	HEADER 1 & HEADER 2 & HEADER 3 \\
	\hline
	\tablelineWORKS{2}{this line works!}
	
	\tablelineNOTWORKS{2}{OK! NO optional param}
	\tablelineNOTWORKS[xxxx]{2}{OK! WITH optional param}

	\tablelineWORKS{2}{this line works!}
	\hline
	\end{tabular}


	This table is not working:		

	\begin{tabular}{ccc} 
	\hline
	HEADER 1 & HEADER 2 & HEADER 3 \\
	\hline
	\tablelineWORKS{2}{this line works!}
	
	\tablelineNOTWORKS{2}{BAD LINE! NO optional param}
	\tablelineNOTWORKS[xxxx]{2}{BAD LINE! WITH optional param}

	\tablelineWORKS{2}{this line works!}
	\hline
	\end{tabular}	
	
\end{document}
The only table that shows up correctly is the middle one, all the other tables have the first column non centered. In my document I use a 'longtable' however during creation on the MWE I realised that the normal table environment does it too, in some configurations, so I left that in!

Im sure it has something to do with the fact that the command \tablelineNOTWORKS has an optional parameter, but I don't understand why that would cause a problem.

I can get round this issue by using two commands instead of one clever one with an optional argument but I would still like to know why the problem exists, if possible.
Any ideas anyone?

Many thanks in advance

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

longtable / table column alignment

Post by rais »

NChairFix wrote:

Code: Select all

\newcommand{\tablelineWORKS}[2]
{
	\doline{#1}{#2}
}
a newline after an opening/closing brace is like entering a space character in the code.

Code: Select all

\documentclass[]{report}

\usepackage{array}
\usepackage{longtable}

% create my new counter
\newcounter{mycounter}
\setcounter{mycounter}{0}

% #1 inc.count
% #2 table column
\newcommand{\doline}[2]
{%
         \arabic{mycounter} & #2 & ???? \addtocounter{mycounter}{#1} \\
}

% #1 = inc. count
% #2 = table column
\newcommand{\tablelineWORKS}[2]
{%
        \doline{#1}{#2}%
}

% #1 = optional argument
% #2 = inc. count
% #3 = table column
\newcommand{\tablelineNOTWORKS}[3][empty]
{%
        \doline{#2}{#3}%
}

\begin{document}
        This long table doesn't work:   
        
        \begin{longtable}{>{\centering\arraybackslash}m{1.5cm}>{\centering\arraybackslash}m{6cm}m{2cm}} 
        \hline
        HEADER 1 & HEADER 2 & HEADER 3 \\
        \hline
        \tablelineWORKS{2}{this line works!}
        
        \tablelineNOTWORKS{2}{BAD LINE! NO optional param}
        \tablelineNOTWORKS[xxxx]{2}{BAD LINE! WITH optional param}

        \tablelineWORKS{2}{this line works!}
        \hline
        \end{longtable}


        This table is working:          
        
        \begin{tabular}{>{\centering\arraybackslash}m{1.5cm}>{\centering\arraybackslash}m{6cm}m{2cm}} 
        \hline
        HEADER 1 & HEADER 2 & HEADER 3 \\
        \hline
        \tablelineWORKS{2}{this line works!}
        
        \tablelineNOTWORKS{2}{OK! NO optional param}
        \tablelineNOTWORKS[xxxx]{2}{OK! WITH optional param}

        \tablelineWORKS{2}{this line works!}
        \hline
        \end{tabular}


        This table is not working:              

        \begin{tabular}{ccc} 
        \hline
        HEADER 1 & HEADER 2 & HEADER 3 \\
        \hline
        \tablelineWORKS{2}{this line works!}
        
        \tablelineNOTWORKS{2}{BAD LINE! NO optional param}
        \tablelineNOTWORKS[xxxx]{2}{BAD LINE! WITH optional param}

        \tablelineWORKS{2}{this line works!}
        \hline
        \end{tabular}   
        
\end{document}
does this look better?

KR
Rainer
NChairFix
Posts: 7
Joined: Thu May 21, 2015 9:08 am

Re: longtable / table column alignment

Post by NChairFix »

Is that really all that it was? :oops:
Blimey! Well thank you for that it has indeed fixed my issue!

I take it that is why I keep seeing % after } all over other folks code? I assumed that the % and everything after it was just ignored, I take it the tex parser ignores EVERYTHING till the end of the line INCLUDING the end of the line character(s)?
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

longtable / table column alignment

Post by rais »

NChairFix wrote:I take it that is why I keep seeing % after } all over other folks code?
Yes.
NChairFix wrote: I assumed that the % and everything after it was just ignored, I take it the tex parser ignores EVERYTHING till the end of the line INCLUDING the end of the line character(s)?
Yes, it won't put a space character in the result.
Consider this small sample:

Code: Select all

\documentclass{article}
\begin{document}
a
    b%
    c
\end{document}
You should see `a bc' in the output: the newline after the `a' character inserts a space, the spaces before the `b' and `c' characters are ignored (at the beginning of their respective lines), and the space (from newline) after the `b' is commented out.

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

longtable / table column alignment

Post by Johannes_B »

rais wrote:

Code: Select all

\documentclass{article}
\begin{document}
a
    b%
    c
\end{document}
I have seen people do the following wondering themselves thy there is a space, even though the end of line is commented out.
Of course, those spaces are counted.

Code: Select all

\documentclass{article}
\begin{document}
a
    b%
    c    %
d
\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.
NChairFix
Posts: 7
Joined: Thu May 21, 2015 9:08 am

Re: longtable / table column alignment

Post by NChairFix »

This it all makes perfect sense and is all quite logical now you mention it, I guess it is something else to get used to and look out for in the future.
Post Reply