Increment (or decrement) a parameter in LaTeX

  • Context: LaTeX 
  • Thread starter Thread starter robbins
  • Start date Start date
  • Tags Tags
    Latex Parameter
Click For Summary
SUMMARY

This discussion addresses how to increment or decrement parameters in LaTeX environments, specifically focusing on creating a custom environment that accepts a single parameter while automatically calculating a second parameter. The solution provided by Joseph Wright involves using a TeX count register to manage the arithmetic operations. By defining a new count with \newcount and utilizing \advance, users can effectively manipulate parameters within the environment definition, allowing for dynamic table creation based on a single input.

PREREQUISITES
  • Understanding of LaTeX environments and syntax
  • Familiarity with TeX count registers
  • Basic knowledge of LaTeX commands like \newcount and \advance
  • Experience with tabular environments in LaTeX
NEXT STEPS
  • Explore advanced LaTeX environment creation techniques
  • Learn about LaTeX counters and their applications
  • Investigate the use of \newcount and \advance in LaTeX
  • Study dynamic table generation in LaTeX using parameters
USEFUL FOR

LaTeX users, document designers, and anyone looking to create flexible and dynamic table structures in their LaTeX documents.

robbins
Messages
7
Reaction score
0
How can I increment or decrement a parameter in LaTeX?

For example, suppose I create an environment foo as follows:

\newenvironment{foo}[2]{\begin{tabular}{*{#1}c*{#2}r}}{\end{tabular}}

I can write

\begin{foo}{4}{3}
<tabular data in seven columns>
\end{foo}

But in my application, if the first parameter is n, then second is always n-1. Is there a way to write this that passes only one parameter (in this case n), but which will produce the table with n + (n - 1) columns as in the environment foo?

Decrementing the parameter #1 inside the newenvironment -- if it is possible -- would surely work, but I don't know how to do it.

Note: The snippet
\let\temp#1
\advance\temp by -1
doesn't work because if I pass the parameter 4, \temp takes the value 'the character 4', which can't follow the \advance command.

I'm stuck. Any help?
 
Physics news on Phys.org
This solution was posted by Joseph Wright on LaTeX Community:

"You don't want \let, you want to do things with numbers proper. The thing is that they then [need] to be assigned to TeX counters. You seem to want something like

\newcount\mycount
\newenvironment{foo}[1]
{%
\mycount #1\relax
\advance\mycount -1\relax
\begin{tabular}{*{#1}c*{\the\mycount}r}%
}
{\end{tabular}}

Here, I'm using a TeX count register to do the maths. You can do the same with a LaTeX counter, but I find this route a bit easier for going downward. TeX assigns count registers locally, so the above should work in most cases."

Thanks, Joseph.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 0 ·
Replies
0
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
11K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K