Graphics, Figures & TablesPredefined text/commands in table cells

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
eiterorm
Posts: 24
Joined: Fri Dec 25, 2009 6:40 pm

Predefined text/commands in table cells

Post by eiterorm »

Hi!

Is there a way of inserting a predefined text and/or command into every cell of a column?

In my case, I wish to create a table where every row is numbered in the first column, but I wish to do so without actually writing anything in the first cell of every row. With a command like the one in question, I could solve my problem using the following code only with a few adjustments:

Code: Select all

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xtab}
\usepackage[table]{xcolor}
% insert package

\begin{document}

\newcounter{counter}
\setcounter{counter}{1}
% insert command
\begin{center}
\begin{xtabular}{ % insert command
r<{\addtocounter{counter}{1}} l l }
& item1 & item2 \\
& item3 & item4 \\
% and so on
\end{xtabular}
\end{center}

\end{document}
I tried solving my problem by writing >{\value{counter}} where the second % insert command text is placed in my code above. This naturally doesn't work, as this executes the command before the column cells, and not inside them. So, is there a way I can get this command to be executed inside the table cells instead?

Thanks
Last edited by eiterorm on Sat Apr 24, 2010 3:41 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Predefined text/commands in table cells

Post by localghost »

That can be done with a special column type that contains a counter for the row number. Note that the first column has to be left blank in the body of the table.

Code: Select all

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{array}

\newcounter{rownum}
\newcolumntype{N}{>{\stepcounter{rownum}\therownum.}c}

\begin{document}
  \begin{tabular}{Nll}\hline
    & Column 2 & Column 3 \\
    & Column 2 & Column 3 \\
    & Column 2 & Column 3 \\
    & Column 2 & Column 3 \\ \hline
  \end{tabular}
\end{document}

Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
eiterorm
Posts: 24
Joined: Fri Dec 25, 2009 6:40 pm

Re: Predefined text/commands in table cells

Post by eiterorm »

This indeed does the trick.
Thanks!
Post Reply