How to replace space with '+' symbol in a triangular array?

Just for fun: without packages.

\documentclass{article}
\newcounter{pft}
\newcounter{pfft}
\begin{document}
\setcounter{pft}{0}\loop%
\stepcounter{pft}%
\setcounter{pfft}{0}{\noindent$\loop%
\stepcounter{pfft}\number\value{pfft}%
\ifnum\number\value{pfft}<\number\value{pft}+\repeat$\par}%
\ifnum\number\value{pft}<11\repeat%
\end{document}

enter image description here

Or with pgffor, which contains the \foreach command that is used in JouleV's nice answer, and which is loaded by TikZ.

\documentclass{article}
\usepackage{pgffor}
\begin{document}
\foreach \X in {1,...,11}%
{\noindent$\foreach \Y in {1,...,\X}%
{\ifnum\Y>1%
+%
\fi%
\Y
}$\par}
\end{document}

Since everyone has a bonus...

\documentclass{article}
\newcounter{pft}
\newcounter{pfft}
\newcounter{sum}
\begin{document}
\begin{flushright}
\setcounter{pft}{0}\loop%
\stepcounter{pft}%
\setcounter{pfft}{0}\setcounter{sum}{0}{\noindent$\loop%
\stepcounter{pfft}\number\value{pfft}\addtocounter{sum}{\value{pfft}}%
\ifnum\number\value{pfft}<\number\value{pft}+\repeat=\number\value{sum}%
\ifnum\value{sum}<10\relax\phantom{1}\fi$\par}%
\ifnum\number\value{pft}<11\repeat%
\end{flushright}
\end{document}

enter image description here

Of course, you can also make this work with align by using a recursion (which I learned from David Carlisle in the chat, if I remember correctly, but my realization is much more elegant because instead of zz, which is deprecated, I use pft ;-).

\documentclass{article}
\newcounter{pft}
\newcounter{pfft}
\newcounter{sum}
\usepackage{amsmath}
\def\Pft{\stepcounter{pft}\number\value{pft}\addtocounter{sum}{\number\value{pft}}\ifnum\number\value{pft}<\number\value{pfft}%
+\Pft\else%
&=\number\value{sum}\\
\fi}
\def\Pfft{\stepcounter{pfft}\setcounter{pft}{0}\setcounter{sum}{0}\Pft%
\ifnum\number\value{pft}<11\relax%
\Pfft%
\fi}
\begin{document}
\setcounter{pfft}{0}
\begin{align*}
\Pfft
\end{align*}
\end{document}

enter image description here


Let's employ TikZ and some simple algorithms for such things.

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[x=5ex,y=5ex]
\def\maxnum{11} % Change the size here
\foreach \i in {1,...,\maxnum} {
    % Draw the numbers
    \pgfmathsetmacro\auxnum{\maxnum-\i+1}
    \foreach \j in {1,...,\auxnum} 
        \node (\i-\j) at (\i,\j) {\i};
    % Draw the plus signs
    \ifnum\i=\maxnum\relax\else
        \pgfmathsetmacro\auxnum{\maxnum-\i}
        \foreach \j in {1,...,\auxnum} 
            \node[anchor=base] at ([shift={(.5,0)}]\i-\j.base) {+};
    \fi
}
\end{tikzpicture}
\end{document}

enter image description here

A little bonus:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[x=5ex,y=5ex]
\def\maxnum{11}
\foreach \i in {1,...,\maxnum} {
    % Draw the numbers
    \pgfmathsetmacro\auxnum{\maxnum-\i+1}
    \foreach \j in {1,...,\auxnum} 
        \node (\i-\j) at (\i,\j) {\i};
    % Draw the plus signs
    \ifnum\i=\maxnum\relax\else
        \pgfmathsetmacro\auxnum{\maxnum-\i}
        \foreach \j in {1,...,\auxnum} 
            \node[anchor=base] at ([shift={(.5,0)}]\i-\j.base) {+};
    \fi
    % The rest :)
    \pgfmathsetmacro\sumoutput{int((\maxnum-\i+1)*(\maxnum-\i+2)/2)}
    \node (output-\i) at (\maxnum+1,\i) {\sumoutput};
    \node[anchor=base] at ([shift={(-.5,0)}]output-\i.base) {=};
}
\end{tikzpicture}
\end{document}

enter image description here


Not sure what do you really want to get by "to the right", but maybe this?

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[x=5ex,y=5ex]
\def\maxnum{11}
\foreach \i in {1,...,\maxnum} {
    % numbers - simpler than the above code
    \foreach \j [count=\k] in {\i,...,1}
        \node (\i-\j) at (\i,\j) {\k};
    % plus signs - a bit simpler
    \ifnum\i=\maxnum\relax\else
        \foreach \j in {\i,...,1} 
            \node[anchor=base] at ([shift={(.5,0)}]\i-\j.base) {+};
    \fi
    % I keep the "bonus" part - it needs no improvements
    \pgfmathsetmacro\sumoutput{int((\maxnum-\i+1)*(\maxnum-\i+2)/2)}
    \node (output-\i) at (\maxnum+1,\i) {\sumoutput};
    \node[anchor=base] at ([shift={(-.5,0)}]output-\i.base) {=};
}
\end{tikzpicture}
\end{document}

enter image description here


Comparison with other answers

Other answers are very good, but it seems that this answer is totally different from all other answers (which are somehow a little similar to each other). This is the pros and cons of this answer compared to the others. Read it to consider what to use.

Pros

The numbers and signs are aligned in a perfect square. So you can have something like this

\documentclass[tikz]{standalone}
\usetikzlibrary{backgrounds,fit,calc}
\begin{document}
\begin{tikzpicture}[x=5ex,y=5ex]
\def\maxnum{11} % Change the size here
\foreach \i in {1,...,\maxnum} {
    % numbers - simpler than the above code
    \foreach \j [count=\k] in {\i,...,1}
        \node[minimum size=.55cm] (\i-\j) at (\i,\j) {\k};
    % plus signs - a bit simpler
    \ifnum\i=\maxnum\relax\else
        \foreach \j in {\i,...,1} 
            \node[anchor=base] at ([shift={(.5,0)}]\i-\j.base) {+};
    \fi
    % I keep the "bonus" part - it needs no improvements
    \pgfmathsetmacro\sumoutput{int((\maxnum-\i+1)*(\maxnum-\i+2)/2)}
    \node (output-\i) at (\maxnum+1,\i) {\sumoutput};
    \node[anchor=base] at ([shift={(-.5,0)}]output-\i.base) {=};
}
\begin{scope}[on background layer]
\path[fill=red!10,draw=red,thick] 
    ([shift={( .1,-.1)}]1-1.north west) -- 
    ([shift={( .1,-.1)}]11-11.north west) 
    arc (135:-45:{.55/sqrt(2)}) --
    ([shift={(-.1, .1)}]1-1.south east) 
    arc (315:135:{.55/sqrt(2)}); % Well, possibly this is not the best
                                 % way to draw this, but this is only
                                 % for illustration purpose.
\draw[red,thick,<-] 
    ($([shift={( .1,-.1)}]1-1.north west)!.45!([shift={( .1,-.1)}]11-11.north west)$)
    -- + (-2,1) node[pos=1.3] {Looks! All are number 1!};
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

In other answers, the numbers, say 1, are not perfectly aligned if \maxnum is greater than 9, so it is harder to draw such a path.

Cons

It is very clear: the spacing between the numbers and the plus signs are inconsistent. But when I follow this approach (insert the plus signs separately), I have to accept this kind of spacing.

Btw, the numbers after the equal signs are not right-aligned. This is fixable, but a fix will make the code much more complicated (if I want to automatically determine the width of the node of the "sum"). I don't think this is a major problem though.


Here's a LuaLaTeX-based solution. The number of rows can be set as the argument of the LaTeX macro \makearray; this macro calls the Lua function make_array, which does most of the work.

enter image description here

To make the triangular array right-aligned instead of left-aligned, change \begin{array}{@{}l@{}} to \begin{array}{@{}r@{}}. If you wish to have a bit more space around the + symbols, change tex.sprint ( "{+}" ) to tex.sprint ( "+" ).

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode} % for 'luacode' environment

%% Lua-side code: Define the Lua function 'make_array'
\begin{luacode}
function make_array ( n )
  for i=1,n do
    for j=1,i do
      tex.sprint ( j )
      if j<i then 
        tex.sprint ( "{+}" )
      else
        tex.sprint ( "\\\\" )
      end
    end
  end
end
\end{luacode}

%% LaTeX-side code: Define the macro '\makearray'
\newcommand\makearray[1]{%
    $\begin{array}{@{}l@{}}
    \directlua{make_array(#1)}
    \end{array}$
}

\begin{document}
\makearray{11}
\end{document}

Addendum: Here's a solution which typesets each row's sum along the right-hand edge of the (now rectangular) array. Compared with the solution shown above, it also allows for a bit more space around the + symbols. Observe the use of \toprule and bottomrule: These instruction are optional and may be left off.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode}  % for '\luaexec' macro
\usepackage{booktabs} % for '\toprule' and '\bottomrule' macros

%% Lua-side code: Define the function 'make_array_with_rowsum'
\luaexec{%
function make_array_with_rowsum ( n )
  for i=1,n do
    for j=1,i do
      tex.sprint ( j )
      if j<i then 
        tex.sprint ( "+" )
      else
        tex.sprint ( "&{{}={}}&" .. i*(i+1)//2 .. "\\\\" )
      end
    end
  end
end
}

%% LaTeX-side code: Define the macro '\makearray'
\newcommand\makearray[1]{%
   \par\noindent
   \begingroup
   \centering
   \setlength\arraycolsep{0pt}
   $\begin{array}{lcr}
      \toprule
      \luadirect{make_array_with_rowsum(#1)}
      \bottomrule
   \end{array}$\par
   \endgroup}

\begin{document}
\makearray{16}
\end{document}

Tags:

Diagrams