Graphics, Figures & TablesLongtables :: Setting 'global' properties

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
James Bejon
Posts: 1
Joined: Fri Jan 02, 2015 3:28 pm

Longtables :: Setting 'global' properties

Post by James Bejon »

Dear All,

I’m completely new to tex. My level of ‘expertise’ is currently pasting text into TexShop (I have a Mac) and pressing “Typeset”. I’m hoping someone might be able to help me with a ‘long table’ related question. I want to set some general properties for my longtables so that their top row is always dark grey and so that they’re always horizontally-centred on my pages. I’ve found some documentation on longtables (http://tug.ctan.org/macros/latex/requir ... gtable.pdf), but I don’t yet have the know-how to make sense of it. I'd be very grateful if someone could give me a hand. Below is some code showing where I've got to so far.

James.

Code: Select all

% Type of doc.
\documentclass[a4paper]{book}


% General packages
\usepackage[margin=1in]{geometry}
\usepackage[pdftex]{graphicx}
\usepackage[T1]{fontenc}
\usepackage{pslatex}
\usepackage[english]{babel}
\usepackage{cjhebrew}
\usepackage{parskip}
\usepackage{enumerate}


% Set for tables
\usepackage{colortbl}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.95}

\usepackage{longtable,tabu}
\usepackage{array} % for extrarowheight
\renewcommand{\arraystretch}{1.5}



% Start doc.
\begin{document}


% Global font settings
\renewcommand{\rmdefault}{ppl}
\fontencoding{T1}
\fontfamily{ppl}
\fontsize{14}{18}
\selectfont


% Text

\chapter{Preface}

\section{Section 1}

Lots of text here.  Lots of text here.  Lots of text here.  Lots of text here.  Lots of text here.  Lots of text here.  Lots of text here.

\clearpage

\rowcolors{1}{}{lightgray}
\begin{longtable}{|p{7.5cm}|p{7.5cm}|}
\hline

\multicolumn{1}{|c}{\textbf{Col. 1}} & \multicolumn{1}{|c|}{\textbf{Col. 2}} \\ \hline

Text in Col 1. & Text in Col 2. \\ \hline
Text in Col 1. & Text in Col 2. \\ \hline
Text in Col 1. & Text in Col 2. \\ \hline
Text in Col 1. & Text in Col 2. \\ \hline
Text in Col 1. & Text in Col 2. \\ \hline

\hline
\end{longtable}


\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

Harish Kumar
Posts: 2
Joined: Sun Feb 22, 2015 1:39 am

Longtables :: Setting 'global' properties

Post by Harish Kumar »

What you have posted is not a Infominimal working example. Having said that, you are loading some packages which are not necessary to be loaded. For example

Code: Select all

% Set for tables
\usepackage{colortbl}  %% loaded by xcolor in next line, not needed
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.95}

\usepackage{longtable,tabu}   %% tabu is not needed
\usepackage{array} % for extrarowheight    %% not needed
Your first question can be solved by using the \rowcolor macro provided by xcolor

Code: Select all

\rowcolors{1}{gray}{}  %%<-- colour alternate rows with gray colour starting from row 1.
Since you need to colour only the first row, issue \hiderowcolors after the first row.

And regarding your second question of "always horizontally-centred on my pages", some more clarification may be needed. At last, I don't think you can set the colouring properties globally to longtable.

Code: Select all

\documentclass[a4paper]{book}
\usepackage[table]{xcolor}
\usepackage{longtable}
\renewcommand{\arraystretch}{1.5}
\begin{document}

\rowcolors{1}{gray}{}  %%<-- colour alternate rows with gray colour starting from row 1.
\begin{longtable}{|p{7.5cm}|p{7.5cm}|}
\hline
\multicolumn{1}{|c|}{\textbf{Col. 1}} & \multicolumn{1}{c|}{\textbf{Col. 2}} \\ \hline
\hiderowcolors         %%% <--- hide row colours after this line
Text in Col 1. & Text in Col 2. \\ \hline
Text in Col 1. & Text in Col 2. \\ \hline
Text in Col 1. & Text in Col 2. \\ \hline
Text in Col 1. & Text in Col 2. \\ \hline
Text in Col 1. & Text in Col 2. \\ \hline
\hline
\end{longtable}
\end{document}
Post Reply