1

I am k novice in LaTeX. I have a tex document that has a cls tex format sheet beside it and create journal articles. Is it possible to make a change that affect all the tables in document have bold header row? If yes which part of cls file should change? There is only one part that refer to tables and figures so I think that the change should be happen here. Thank you in advance for your help.

% Graphics, tables and other formatting 
\RequirePackage{graphicx,xcolor} 
\RequirePackage{colortbl} 
\RequirePackage{booktabs} 
 \RequirePackage{algorithm} 
\RequirePackage[noend]{algpseudocode} 
\RequirePackage{changepage} 
\RequirePackage[left=48pt,% right=42pt,% top=46pt,% bottom=60pt,% 
                headheight=15pt,% headsep=10pt,% letterpaper,twoside]
                {geometry}% 
\RequirePackage[labelfont={bf,sf},% 
                labelsep=period,% 
                figurename=Fig.,% 
                singlelinecheck=off,% 
                justification=RaggedRight]{caption} 
\setlength{\columnsep}{24pt} % Distance between the two columns of text 
\setlength{\parindent}{12pt} % 

Paragraph indent

5
  • 1
    Welcome to TeX.SX! Do you really have this all in one line?
    – TeXnician
    Nov 20, 2017 at 12:55
  • If you are talking about submitting to a journal, check first whether it allows this. If yes, you may want to have a look at the first link under "Related" on the right.
    – TeXnician
    Nov 20, 2017 at 13:13
  • 1
    latex has no specific markup for table headers, it is just the first row or rows of a table, so there is no way to specify this globally. The code shown isn't related to the table itself, just the caption text. Nov 20, 2017 at 13:58
  • @Amir: So you're interested in formatting the whatever is written between \begin{tabular}[.]{..} and the first \\ as being the header. So this excludes things like longtable, tabularx, array, ...
    – Werner
    Nov 20, 2017 at 20:17
  • @Amir: ...what does this formatting have to look like? What should be done with the content provided by the user? For example, if someone supplies \begin{tabular}[.]{..} \hline A & B & C & ... \\ ... \end{tabular}, what should that be changed into?
    – Werner
    Nov 20, 2017 at 20:40

2 Answers 2

1

The following updates the tabular environment and assumes the first line - everything up to the first \\ - contains the header. The header is updated to insert whatever you supply to \setheaderformat{<format>} at the start of each cell, taking care to always keep \hline (or \toprule, from booktabs) as the first entry within the header. \multicolumn header entries are partially supported (via \mc; see below), but nothing else (like multi-line headers, or whatever).

enter image description here

\documentclass{article}

\usepackage{environ,etoolbox,regexpatch,booktabs}

\let\oldtabular\tabular
\let\endoldtabular\endtabular

\makeatletter
\newcommand{\setheaderformat}[1]{\def\header@format{#1}}

\def\format@header#1\\#2\relax{%
  \def\tabular@header{#1\\}%
  \def\tabular@other{#2\relax\relax}%
  \patchcmd{\tabular@header}{\hline}{\hline\TAB}{}{}% \hline support (insert \TAB *after* \hline)
  \patchcmd{\tabular@header}{\toprule}{\toprule\TAB}{}{}% \toprule support (for booktabs)
  \patchcmd{\tabular@other}{\\\relax\relax}{}{}{}% Remove possible ending \\
  \patchcmd{\tabular@other}{\relax\relax}{}{}{}%
}
\RenewEnviron{tabular}[2][c]{%
  \expandafter\format@header\BODY\\\relax% Extract header/other components
  \gdef\TAB{\header@format\gdef\TAB{& \header@format\ignorespaces}\ignorespaces}% Update how \TAB will work
  \xpatchcmd*{\tabular@header}{&}{\TAB}{}{}% Replace all & with \TAB in header
  \begin{oldtabular}[#1]{#2}
    \tabular@header
    \tabular@other
  \end{oldtabular}%
}
\makeatother

\setheaderformat{\bfseries}

\begin{document}

\begin{oldtabular}{ *{3}{c} }
  \hline
  a & b & c \\
  \hline
  d & e & f \\
  g & h & i \\
  \hline
\end{oldtabular}
\qquad
\begin{tabular}{ *{3}{c} }
  \hline
  a & b & c \\
  \hline
  d & e & f \\
  g & h & i \\
  \hline
\end{tabular}

\bigskip

\begin{oldtabular}{ *{3}{c} }
  \toprule
  a & b & c \\
  \midrule
  d & e & f \\
  g & h & i \\
  \bottomrule
\end{oldtabular}
\qquad
\begin{tabular}{ *{3}{c} }
  \toprule
  a & b & c \\
  \midrule
  d & e & f \\
  g & h & i \\
  \bottomrule
\end{tabular}

\bigskip

% How to manage \multicolumn
\newcommand{\mc}{& \multicolumn}

\setheaderformat{\itshape$\rightarrow$}%
\begin{oldtabular}{ *{3}{c} }
  \toprule
  a & b & \multicolumn{1}{r}{\bfseries c$\leftarrow$} \\
  \midrule
  d & e & f \\
  g & h & i \\
  \bottomrule
\end{oldtabular}
\qquad
\begin{tabular}{ *{3}{c} }
  \toprule
  a & b \mc{1}{r}{\bfseries c$\leftarrow$} \\
  \midrule
  d & e & f \\
  g & h & i \\
  \bottomrule
\end{tabular}

\end{document}
1
  • 1
    It seems like a better idea to format the headers without such automated intervention, since you don't know what people might put in their headers...
    – Werner
    Nov 21, 2017 at 1:26
1

FWIW, in ConTeXt this is relatively straight forward to achieve. To make the first row of the table bold, you can use

\setupTABLE[row][first][style=bold]

However, it is not a good idea to make this change globally because there may be instances where you want the first row not to be bold. Instead, it is better to define a new setup and use that in tables:

\startsetups table:header
    \setupTABLE[row][first][style=bold]
\stopsetups

and then use it as:

\startTABLE[setups=table:header]

Here is a complete example showing the result.

\startsetups table:header
  \setupTABLE[each][each][frame=off, offset=0.25ex, rulethickness=1.5pt]
  \setupTABLE[row][first][style=bold, topframe=on, bottomframe=on]
  \setupTABLE[row][last] [bottomframe=on]
\stopsetups

\starttext

\startTABLE[setups=table:header]
  \NC Type \NC First \NC Second \NC Third \NC \NR
  \NC A    \NC B     \NC C      \NC D     \NC \NR
  \NC A    \NC B     \NC C      \NC D     \NC \NR
  \NC A    \NC B     \NC C      \NC D     \NC \NR
  \NC A    \NC B     \NC C      \NC D     \NC \NR
\stopTABLE

\stoptext

which gives

enter image description here

Unfortunately, as you have found out, none of the table mechanisms in LaTeX provide such a separation between content and presentation in tables.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .