12

How can I make number pattern like this

enter image description here

But, all spaces are replaced with plus signs ?

  • 2
    Welcome to TeX.SE! – Mensch May 5 at 4:03
  • 4
    In the final row of your screenshot, should there be a space between 10 and 11? – Mico May 5 at 4:12
16

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.

  • What if you want to make it align to the right ? What is this TikZ thing ? (I'm a very-beginner) – LimoPanda May 5 at 4:05
  • 2
    Hello @LimoPanda, if you want to know what TikZ is good for, please have a look at p. 18 of this manual. ;-) – user121799 May 5 at 4:10
  • @LimoPanda Sorry for the late reply. What do you want to align to the right? – user156344 May 5 at 4:34
  • 1
    @marmot I thought it should be pages 18 and 11-12 of that manual – user156344 May 5 at 4:39
  • 1
    @Mico Sure. I edited my answer. – user156344 May 5 at 14:06
24

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

11

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}
10
\documentclass{article}
\usepackage{multido}
\begin{document}
\noindent$1$
\multido{\iA=2+1}{10}{\\$%
  \multido{\iB=1+1}{\numexpr\iA-1}{\iB+}\iA$}

\bigskip\noindent$1=1$
\multido{\iA=2+1}{10}{\\$\xdef\Sum{0}%
    \multido{\iB=1+1}{\numexpr\iA-1}{%
        \iB+\xdef\Sum{\the\numexpr\Sum+\iB}}\iA=\the\numexpr\Sum+\iA$}

\end{document}

enter image description here

7

The mandatory double expl3 loop:

\documentclass{article}
\usepackage{xparse,array}

\ExplSyntaxOn
\NewDocumentCommand{\triangletab}{sm}
 {% #1 = * for totals, #2 = end point
  \IfBooleanTF { #1 }
   {
    \begin{tabular}{@{} l @{} c @{} r @{}}
    \int_step_inline:nn { #2 } { \panda_row_total:n { ##1 } \\ }
    \end{tabular}
   }
   {
    \begin{tabular}{@{} l @{}}
    \int_step_inline:nn { #2 } { \panda_row:n { ##1 } \\ }
    \end{tabular}
   }
 }
\cs_new_protected:Nn \panda_row:n
 {
  $1 \int_step_inline:nnn { 2 } { #1 } { + ##1 }$
 }
\cs_new_protected:Nn \panda_row_total:n
 {
  \panda_row:n { #1 } & ${}={}$ & $\int_eval:n { (#1)*(#1+1)/2 }$
 }
\ExplSyntaxOff

\begin{document}

\[
\triangletab{1} \qquad \triangletab{3} \qquad \triangletab{11}
\]

\[
\triangletab*{1} \qquad \triangletab*{3} \qquad \triangletab*{11}
\]

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.