LaTeX forum ⇒ Graphics, Figures & Tableslongtable / table column alignment Topic is solved

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

longtable / table column alignment

Postby NChairFix » Mon Jun 08, 2015 2:15 pm

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:

  1. \documentclass[]{report}
  2.  
  3. \usepackage{array}
  4. \usepackage{longtable}
  5.  
  6. % create my new counter
  7. \newcounter{mycounter}
  8. \setcounter{mycounter}{0}
  9.  
  10. % #1 inc.count
  11. % #2 table column
  12. \newcommand{\doline}[2]
  13. {
  14. \arabic{mycounter} & #2 & ???? \addtocounter{mycounter}{#1} \\
  15. }
  16.  
  17. % #1 = inc. count
  18. % #2 = table column
  19. \newcommand{\tablelineWORKS}[2]
  20. {
  21. \doline{#1}{#2}
  22. }
  23.  
  24. % #1 = optional argument
  25. % #2 = inc. count
  26. % #3 = table column
  27. \newcommand{\tablelineNOTWORKS}[3][empty]
  28. {
  29. \doline{#2}{#3}
  30. }
  31.  
  32. \begin{document}
  33. This long table doesn't work:
  34.  
  35. \begin{longtable}{>{\centering\arraybackslash}m{1.5cm}>{\centering\arraybackslash}m{6cm}m{2cm}}
  36. \hline
  37. HEADER 1 & HEADER 2 & HEADER 3 \\
  38. \hline
  39. \tablelineWORKS{2}{this line works!}
  40.  
  41. \tablelineNOTWORKS{2}{BAD LINE! NO optional param}
  42. \tablelineNOTWORKS[xxxx]{2}{BAD LINE! WITH optional param}
  43.  
  44. \tablelineWORKS{2}{this line works!}
  45. \hline
  46. \end{longtable}
  47.  
  48.  
  49. This table is working:
  50.  
  51. \begin{tabular}{>{\centering\arraybackslash}m{1.5cm}>{\centering\arraybackslash}m{6cm}m{2cm}}
  52. \hline
  53. HEADER 1 & HEADER 2 & HEADER 3 \\
  54. \hline
  55. \tablelineWORKS{2}{this line works!}
  56.  
  57. \tablelineNOTWORKS{2}{OK! NO optional param}
  58. \tablelineNOTWORKS[xxxx]{2}{OK! WITH optional param}
  59.  
  60. \tablelineWORKS{2}{this line works!}
  61. \hline
  62. \end{tabular}
  63.  
  64.  
  65. This table is not working:
  66.  
  67. \begin{tabular}{ccc}
  68. \hline
  69. HEADER 1 & HEADER 2 & HEADER 3 \\
  70. \hline
  71. \tablelineWORKS{2}{this line works!}
  72.  
  73. \tablelineNOTWORKS{2}{BAD LINE! NO optional param}
  74. \tablelineNOTWORKS[xxxx]{2}{BAD LINE! WITH optional param}
  75.  
  76. \tablelineWORKS{2}{this line works!}
  77. \hline
  78. \end{tabular}
  79.  
  80. \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

Tags:

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

Postby rais » Mon Jun 08, 2015 4:30 pm

NChairFix wrote:
  1. \newcommand{\tablelineWORKS}[2]
  2. {
  3. \doline{#1}{#2}
  4. }


a newline after an opening/closing brace is like entering a space character in the code.
  1. \documentclass[]{report}
  2.  
  3. \usepackage{array}
  4. \usepackage{longtable}
  5.  
  6. % create my new counter
  7. \newcounter{mycounter}
  8. \setcounter{mycounter}{0}
  9.  
  10. % #1 inc.count
  11. % #2 table column
  12. \newcommand{\doline}[2]
  13. {%
  14. \arabic{mycounter} & #2 & ???? \addtocounter{mycounter}{#1} \\
  15. }
  16.  
  17. % #1 = inc. count
  18. % #2 = table column
  19. \newcommand{\tablelineWORKS}[2]
  20. {%
  21. \doline{#1}{#2}%
  22. }
  23.  
  24. % #1 = optional argument
  25. % #2 = inc. count
  26. % #3 = table column
  27. \newcommand{\tablelineNOTWORKS}[3][empty]
  28. {%
  29. \doline{#2}{#3}%
  30. }
  31.  
  32. \begin{document}
  33. This long table doesn't work:
  34.  
  35. \begin{longtable}{>{\centering\arraybackslash}m{1.5cm}>{\centering\arraybackslash}m{6cm}m{2cm}}
  36. \hline
  37. HEADER 1 & HEADER 2 & HEADER 3 \\
  38. \hline
  39. \tablelineWORKS{2}{this line works!}
  40.  
  41. \tablelineNOTWORKS{2}{BAD LINE! NO optional param}
  42. \tablelineNOTWORKS[xxxx]{2}{BAD LINE! WITH optional param}
  43.  
  44. \tablelineWORKS{2}{this line works!}
  45. \hline
  46. \end{longtable}
  47.  
  48.  
  49. This table is working:
  50.  
  51. \begin{tabular}{>{\centering\arraybackslash}m{1.5cm}>{\centering\arraybackslash}m{6cm}m{2cm}}
  52. \hline
  53. HEADER 1 & HEADER 2 & HEADER 3 \\
  54. \hline
  55. \tablelineWORKS{2}{this line works!}
  56.  
  57. \tablelineNOTWORKS{2}{OK! NO optional param}
  58. \tablelineNOTWORKS[xxxx]{2}{OK! WITH optional param}
  59.  
  60. \tablelineWORKS{2}{this line works!}
  61. \hline
  62. \end{tabular}
  63.  
  64.  
  65. This table is not working:
  66.  
  67. \begin{tabular}{ccc}
  68. \hline
  69. HEADER 1 & HEADER 2 & HEADER 3 \\
  70. \hline
  71. \tablelineWORKS{2}{this line works!}
  72.  
  73. \tablelineNOTWORKS{2}{BAD LINE! NO optional param}
  74. \tablelineNOTWORKS[xxxx]{2}{BAD LINE! WITH optional param}
  75.  
  76. \tablelineWORKS{2}{this line works!}
  77. \hline
  78. \end{tabular}
  79.  
  80. \end{document}

does this look better?

KR
Rainer

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

Postby NChairFix » Mon Jun 08, 2015 5:36 pm

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: 235
Joined: Sun Nov 16, 2014 8:51 pm

Postby rais » Tue Jun 09, 2015 5:01 pm

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:
  1. \documentclass{article}
  2. \begin{document}
  3. a
  4. b%
  5. c
  6. \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: 4044
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Tue Jun 09, 2015 6:02 pm

rais wrote:
  1. \documentclass{article}
  2. \begin{document}
  3. a
  4. b%
  5. c
  6. \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.
  1. \documentclass{article}
  2. \begin{document}
  3. a
  4. b%
  5. c %
  6. d
  7. \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

Postby NChairFix » Wed Jun 10, 2015 9:59 am

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.


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 3 guests