Increment (or decrement) a parameter in LaTeX

In summary, the conversation discusses how to increment or decrement a parameter in LaTeX. The problem is that the environment foo requires two parameters, but the second parameter is always one less than the first. The solution involves using a count register to perform the necessary math and assign it to TeX counters. This should work in most cases.
  • #1
robbins
7
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
  • #2
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.
 
  • #3


To increment or decrement a parameter in LaTeX, you can use the \advance command. This command allows you to perform basic arithmetic operations on a parameter. In your case, you can use it to decrement the first parameter passed to your environment foo.

To do this, you can use the following code within your newenvironment definition:

\advance#1 by -1

This will decrement the value of #1 by 1. So, for example, if you pass the value 4 as the first parameter, #1 will now have a value of 3.

In your specific case, you can use this code to decrement the first parameter and then use the decremented value to set the number of columns in your tabular environment. So, your newenvironment definition would look like this:

\newenvironment{foo}[1]{\advance#1 by -1 \begin{tabular}{*{#1}c*{#1-1}r}}{\end{tabular}}

This will allow you to pass only one parameter (n) and still produce a table with n + (n - 1) columns as in the environment foo.

I hope this helps!
 

1. How do I increment or decrement a parameter in LaTeX?

To increment or decrement a parameter in LaTeX, you can use the commands \advance or \multiply, respectively. For example, to increment a counter named "mycounter" by 1, you would use \advance\mycounter by 1. To decrement the counter by 2, you would use \advance\mycounter by -2.

2. Can I use variables to increment or decrement parameters in LaTeX?

Yes, you can use variables in conjunction with the \advance and \multiply commands to increment or decrement parameters in LaTeX. For example, if you have a variable named "myvar" with a value of 3, you can use \advance\mycounter by \myvar to increment the counter by 3.

3. What are some common parameters that can be incremented or decremented in LaTeX?

Some common parameters that can be incremented or decremented in LaTeX include counters, lengths, and dimensions. These are often used to control the layout and formatting of documents, such as page numbering, spacing, and margins.

4. Can I increment or decrement parameters by a specific amount in LaTeX?

Yes, you can specify the amount by which you want to increment or decrement a parameter in LaTeX. This can be a positive or negative value, and can also be a variable. For example, you can use \advance\mycounter by 5 to increment the counter by 5, or \advance\mycounter by -\myvar to decrement the counter by the value of the variable "myvar".

5. Is it possible to increment or decrement parameters in LaTeX within a loop?

Yes, it is possible to increment or decrement parameters in LaTeX within a loop. This can be done using the \loop and \repeat commands, which allow you to specify a condition for the loop to continue, as well as the commands to be executed within the loop. This is often used for automating tasks and generating repetitive elements in documents.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
351
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
946
Replies
0
Views
2K
Back
Top