Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need to use the counter to make some computations inside loops. For instance, I want to write a for loop to get the following:

                               

    

What is the best way to write using \forloop? Using \forloop and \newcounter, I am unable to perform operations on the counter, i.e., I am unable to perform operations like 2*k-1, where k is the counter in the \forloop.

share|improve this question
    
LaTeX's counters are not really for computations (one has to prefix with \c@ the counter name to get the underlying TeX count register). Try \newcount\cnti (once), and \the\numexpr 2*\cnti-1\relax in your constructions (\advance\cnti\@ne) –  jfbu May 15 '13 at 18:58
    
@jfbu Could you expand your comment to an answer with an example? Thanks. –  user1876 May 15 '13 at 18:59
    
Do you just want to display the equations above or are you interested in actually evaluating A_{11} times x_1 plus A_{12} times x_2? We don't know what the matrices A and x hold, if you're after the latter. –  Werner May 15 '13 at 19:05
    
Well, sure, but I need to have a bit more of context, perhaps you need a so-called expandable loop. Do you just want some code to produce the lines as shown in your image? are you going to need it also for, say 77, rather than 10? I will post a tentative answer, which may not fit the bill. I am confident some answers by others will show up quickly also. –  jfbu May 15 '13 at 19:05
    
@Werner I am not interested in evaluating the matrix-vector product, I am just interested in displaying them in the symbolic form. –  user1876 May 15 '13 at 19:21
show 2 more comments

7 Answers

up vote 7 down vote accepted

enter image description here

A combination of forloop and calc:

\documentclass[10pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{calc}
\usepackage{forloop}
\begin{document}

\hsize=8cm
\newcounter{k}\newcounter{j}%
\begin{align}
%forloop[ step ]{ counter }{ initial value }{ condition }{ code }
\nonumber
\forloop{k}{1}{\value{k} < 11}{
\setcounter{j}{2*\value{k}-1}
\\ y_{\arabic{k}}&=A_{\arabic{k},\arabic{j}} x_{\arabic{j}}
\addtocounter{j}{1}
+A_{\arabic{k},\arabic{j}} x_{\arabic{j}}
}
\end{align}

\end{document}
share|improve this answer
    
+1. and thanks. I was looking for something along these lines. –  user1876 May 15 '13 at 20:04
add comment

Here another approach using l3int:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse} %dt. Silbentrennung
\ExplSyntaxOn
\cs_generate_variant:Nn \int_set:Nn { Nx }
\NewDocumentCommand \Formula { O{1} O{1} m }
 {
  \int_step_inline:nnnn
     { #1 }%initial value
     { #2 }%step
     { #3 - #2 }%final value
     { \formula_func_aux:n { ##1 } \\ }%code
  \formula_func_aux:n { #3 } %last line without \\
 }

\cs_new:Npn  \formula_func_aux:n #1
 {
  \int_gset:Nn \g_tmpa_int { #1 }
      y \sb{ \int_use:N \g_tmpa_int } &=
      \int_gset:Nn \g_tmpb_int { 2 * \g_tmpa_int -1  }
      A \sb{ \int_use:N \g_tmpa_int , \int_use:N \g_tmpb_int }
      x \sb{ \int_use:N \g_tmpb_int }
     +
      \int_gset:Nn \g_tmpb_int { 2 * \g_tmpa_int }
      A \sb{ \int_use:N \g_tmpa_int , \int_use:N \g_tmpb_int }
      x \sb{ \int_use:N \g_tmpb_int } 
 }

\ExplSyntaxOff
\begin{document}
\begin{align}
\Formula{2}
\end{align}

\begin{align}
\Formula[1][2]{6}
\end{align}
\end{document}

enter image description here

share|improve this answer
add comment

enter image description here

This also uses no counters but is perhaps a bit simpler than jfbu's but is the same idea.

\documentclass{article}

\usepackage{amsmath}


\newcommand\y[1]{%
y_{\the#1}&=A_{\the#1\,\the\numexpr2*#1-1\relax}
      +A_{\the#1\,\the\numexpr2*#1\relax}
        x_{\the\numexpr2*#1\relax}}

\def\lp#1#2#3{\ifnum#1=#3\relax\stoplp\fi#2{\numexpr#1\relax}\\\lp{\numexpr#1+1\relax}#2{#3}}

\def\stoplp#1\\#2#3#4#5{#1}

\begin{document}

\begin{align}
\lp1\y{10}
\end{align}

\end{document}
share|improve this answer
    
yes, simpler indeed! my answer builds expandably the complete thing and only at the end re-inserts it in the token stream... (professional deformation due to work on a package). –  jfbu May 15 '13 at 20:27
add comment

Here's a way to do it, by using a regular array for alignment:

enter image description here

\documentclass{article}
\usepackage{multido}% http://ctan.org/pkg/multido
\begin{document}
\def\yeqns{}
\[\begin{array}{r@{}l}
  \begingroup
  \let\\\relax
  \multido{\iKa=1+1,\iKb=2+2,\iKc=1+2}{10}{%
    \xdef\yeqns{\yeqns% Gather equations for y
      y_{\iKa} &{}= A_{\iKa,\iKc}x_{\iKc}+A_{\iKa,\iKb}x_{\iKb} \\}}
  \endgroup
  \yeqns% Print equations
\end{array}\]
\end{document}

Technically, there's no calculations performed on the counters and I've separated row/column indices using , (you can change that).

multido helps set up the iterative evaluation of items I called \iKa, \iKb and \iKc (respectively defined as k, 2k and 2k-1).

share|improve this answer
add comment

You see? three answers already, almost immediately. Mine is comparatively tremendously complicated. I started in a certain direction, and wanted to get it done that way.

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand\CreateEquations[2]{%
   \expandafter\@createequations@a\expandafter
   {\the\numexpr #1\expandafter }\expandafter{\the\numexpr #2}%
}%

\def\@createequations@a #1#2%
{%
    \ifnum #1>#2 
          \expandafter\@gobblefour
    \else 
          \expandafter\expandafter\expandafter
          \@createequations@b
    \fi
    \expandafter {\@createoneequation{#1}}{#1}{#2}%
}%

\def\@createequations@b #1#2#3%
{%
    \ifnum #2<#3
        \expandafter\@createequations@c\expandafter
        {\the\numexpr #2+1\expandafter}%
    \fi \@createequations@finish {#1}{#3}%
}%

\def\@createequations@c #1\@createequations@finish
{%
    \expandafter\@createequations@d\expandafter
     {\@createoneequation {#1}}{#1}%
}%

\def\@createequations@d #1#2#3{\@createequations@b {#3\\ #1}{#2}}%

\def\@createequations@finish #1#2{\begin{align}
                                    #1
                                  \end{align}}

\def\@createoneequation #1{%
     y_{#1} &=A_{#1,\the\numexpr 2*#1-1\relax}
                         x_{\the\numexpr 2*#1-1\relax}
                        +A_{#1,\the\numexpr 2*#1\relax}
                         x_{\the\numexpr 2*#1\relax}%
}

\makeatother


\begin{document}

  \CreateEquations {1}{10}

  \CreateEquations {1}{-1}

  \CreateEquations {100}{103}


\end{document}

equations

share|improve this answer
1  
and I forgot to say that this code uses no counters at all... –  jfbu May 15 '13 at 20:00
    
+++1 I was half way through an answer along these lines, but then it was meal time. –  David Carlisle May 15 '13 at 20:03
    
@DavidCarlisle I have to lose weight, this gives me a distinctive advantage... just had a light soup... –  jfbu May 15 '13 at 20:07
    
I decided to finsh mine off and post mine as well, but still +1 to you:-) –  David Carlisle May 15 '13 at 20:23
add comment

A fairly general method, with a "simple macro" and a complex one that's completely customizable.

\documentclass{article}
\usepackage{xparse,amsmath}
\ExplSyntaxOn
% #1 = number of x's
% #2 = number of equations
\NewDocumentCommand{\makeequations}{O{2}m}
 {
  \egreg_makeequations:nnnnnn { x } { y } { A } { 1 } { #1 } { #2 }
 }

\tl_new:N \l_egreg_equations_tl
\seq_new:N \l_egreg_equation_seq

% #1 = variables on RHS
% #2 = variable on LHS
% #3 = coefficient letter
% #4 = starting point
% #5 = number of variables in RHS
% #6 = number of equations
\cs_new_protected:Npn \egreg_makeequations:nnnnnn #1 #2 #3 #4 #5 #6
 {
  \tl_clear:N \l_egreg_equations_tl
  \int_step_inline:nnnn { #4 } { 1 } { #4+#6-1 }
   {
    \egreg_makeequation:nnnnn { #1 } { #2 } { #3 } { #5 } { ##1 }
    \int_compare:nF { ##1 = #4+#6-1 }
     { \tl_put_right:Nn \l_egreg_equations_tl { \\ } }
   }
  \use:x
   {
    \exp_not:n { \begin{align} }
    \exp_not:V { \l_egreg_equations_tl }
    \exp_not:n { \end{align} }
   }
 }

\cs_new_protected:Npn \egreg_makeequation:nnnnn #1 #2 #3 #4 #5
 {
  \tl_put_right:Nn \l_egreg_equations_tl { #2\sb{#5} & = }
  \seq_clear:N \l_egreg_equation_seq
  \int_step_inline:nnnn { #4 * (#5-1) + 1 } { 1 } { #4 * #5 }
   {
    \seq_put_right:Nn \l_egreg_equation_seq
     { 
      #3\sb{#5 \egreg_comma:nn { #5 } { ##1 } ##1}#1\sb{##1}
     }
   }
  \tl_put_right:Nx \l_egreg_equations_tl 
   { \seq_use:Nnnn \l_egreg_equation_seq {+}{+}{+} }
 }
\cs_new:Npn \egreg_comma:nn #1 #2
 {
  \bool_if:nT 
   { \int_compare_p:n {#1 > 9} || \int_compare_p:n { #2 > 9 } }
   { , }
 }

\keys_define:nn { makeequations }
 {
  LHSvar .tl_set:N  = \l_egreg_eqs_lhsvar_tl, LHSvar .initial:n = y,
  RHSvar .tl_set:N  = \l_egreg_eqs_rhsvar_tl, RHSvar .initial:n = x,
  COEFF  .tl_set:N  = \l_egreg_eqs_coeff_tl,  COEFF  .initial:n = A,
  EQS    .int_set:N = \l_egreg_eqs_eqs_int,   
  VARS   .int_set:N = \l_egreg_eqs_vars_int,  VARS   .initial:n = 2,
  START  .int_set:N = \l_egreg_eqs_start_int, START  .initial:n = 1,
 }
\NewDocumentCommand{\xmakeequations}{m}
 {
  \group_begin:
  \keys_set:nn { makeequations } { #1 }
  \egreg_makeequations:VVVVVV
   \l_egreg_eqs_rhsvar_tl
   \l_egreg_eqs_lhsvar_tl
   \l_egreg_eqs_coeff_tl
   \l_egreg_eqs_start_int
   \l_egreg_eqs_vars_int
   \l_egreg_eqs_eqs_int
  \group_end:
 }
\cs_generate_variant:Nn \egreg_makeequations:nnnnnn { VVVVVV }

\ExplSyntaxOff
\begin{document}
\makeequations{3}

\makeequations[3]{4}

\xmakeequations{EQS=3}

\xmakeequations{EQS=4,VARS=3,COEFF=R,LHSvar=p,RHSvar=q,START=100}

\end{document}

enter image description here

In the simple macro you simply state the number of equations and, optionally, the number of variables used in the right hand side.

In the complex macro, with a key-value syntax, you can also change the letters used for the variables and the coefficients, but also the starting point. So, for instance, you can chain calls with

\xmakeequations{EQS=3}
Some text in between
\xmakeequations{EQS=4,START=4}

enter image description here

share|improve this answer
add comment

I propose one more answer, which uses expandable macros from the xint package, version 1.06a or later. Here, the case of an empty set of indices is treated less well than in my other answer (an empty align environment is typeset, which makes an increase in the equation counter), this could be fixed of course. Also, contrarily to my other answer, here the indices must be explicit integers not count registers. This also could be fixed, the macro \PrepareIndices could be changed a bit for that.

The only place where the commands from xint are used is in the \CreateEquations macro.

\documentclass{article}
\usepackage{amsmath}

\usepackage{xint} % http://www.ctan.org/pkg/xint


% cf etex manual top of page 9
% \PrepareIndices {1}{10} returns {1}....{10}
% \PrepareIndices {i}(j} is empty if i>j
% This code is prepared only for explicit integers
\newcommand\PrepareIndices[2]{%
\ifnum #1<#2
    \expandafter\PrepareIndices
    \expandafter{\number\numexpr#1\expandafter}%
    \expandafter{\number\numexpr#2-1\expandafter}%
    \expandafter{\number\numexpr#2\expandafter}%
\else
  \ifnum #1=#2
    \expandafter{\number\numexpr #1\expandafter\expandafter\expandafter}%
  \fi
\fi  }


\newcommand\DoOneEquation[1]{%
     y_{#1} &=A_{#1,\the\numexpr 2*#1-1\relax}
                         x_{\the\numexpr 2*#1-1\relax}
                        +A_{#1,\the\numexpr 2*#1\relax}
                         x_{\the\numexpr 2*#1\relax}%
}


% The next thing uses macros from the xint package, 1.06a or later
% Except from the encapsulation in the align environment, this
% does only completely expandable things
% However, contrarily to my other answers, here when there is
% no equation to typeset because #1>#2, there is still an empty
% align environment created
\newcommand\CreateEquations[2]%
    {\begin{align}
     \xintListWithSep
          {\\}
          {\xintApply{\DoOneEquation}{\PrepareIndices {#1}{#2}}}
     \end{align}}


\begin{document}
  \CreateEquations {1}{5}

  \CreateEquations {1}{-1}

  \CreateEquations {100}{103}

  \CreateEquations{-5}{-5}
\end{document}

equations

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.