Graphics, Figures & Tablesrenewcommand inside table

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
florrat
Posts: 4
Joined: Mon Feb 21, 2011 3:54 pm

renewcommand inside table

Post by florrat »

Hello everyone,

I'm trying to make a table, and I would like to (re)define a command in this table. But for some reason LaTeX forgets that I redefined the command when I move to the next cell. Is there some other way to redefine it, so that this does not happen?

And something else: actually I only need to store a string. Is there some way to store a string other than use commands? Something like a \newstring command in the same way as the \newlength command for lengths?

Thank you!

Minimal working example (okay, maybe not totally minimal, the same happens for tabulars instead of longtables)

Code: Select all

\documentclass[12pt]{article}
\usepackage[english]{babel}
\usepackage{longtable}

\newcommand{\var}{abc}

\begin{document}

\begin{longtable}{c|c}
 \var & \renewcommand{\var}{def} \var \\ \hline
 \var & \var \\ \hline
 \renewcommand{\var}{ghi} \var & \var \\ \hline
 \var \renewcommand{\var}{jkl} & \var \\
\end{longtable}
\end{document} 
Output:
abc def
abc abc
ghi abc
abc abc

Wanted output:
abc def
def def
ghi ghi
ghi jkl
Last edited by florrat on Fri May 06, 2011 2:37 pm, edited 1 time in total.

Recommended reading 2024:

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

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

renewcommand inside table

Post by josephwright »

Each table cell forms a group. If you want material to be altered between groups, you therefore have to use the TeX \gdef macro

Code: Select all

\documentclass[12pt]{article}
\usepackage[english]{babel}
\usepackage{longtable}

\newcommand{\var}{abc}

\begin{document}

\begin{longtable}{c|c}
\var & \gdef\var{def} \var \\ \hline
\var & \var \\ \hline
\gdef\var{ghi} \var & \var \\ \hline
\var \gdef\var{jkl} & \var \\
\end{longtable}
\end{document} 
Joseph Wright
florrat
Posts: 4
Joined: Mon Feb 21, 2011 3:54 pm

Re: renewcommand inside table

Post by florrat »

Thank you very much!
Post Reply