Text FormattingDefining a command with counter

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
artemff
Posts: 113
Joined: Sat Oct 17, 2009 11:15 pm

Defining a command with counter

Post by artemff »

Why this commands don't work?

Code: Select all

\newcommand{\commandrep}[2]
{
 \newcounter{i}
 \setcounter{i}{0}
 \whiledo{\not\equal{\value{i}}{#1}}
  {
   \addtocounter{i}{1}
   #2 
  }
}
 
%leave n empty string;
\newcommand{\printent}[1]
{
 \commandrep{#1}{\newline}
}
I invoke its such:

Code: Select all

\printent{4}
And there an error is appeared.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Defining a command with counter

Post by gmedina »

Hi,

after suitable completion, your command definitions work OK[1] for me. Perhaps you are not loading the ifthen package (that provides the \whiledo command)? Do you have the following line in the preamble of your document?

Code: Select all

\usepackage{ifthen}
If that was not the problem, then please post here a minimal working example that allows us to reproduce the problem.

[1] OK, in this case, means without producing errors; however, your command will produce the typical "Underfull \hbox (badness 10000)..." warning.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
artemff
Posts: 113
Joined: Sat Oct 17, 2009 11:15 pm

Defining a command with counter

Post by artemff »

>> gmedina

Code: Select all

\documentclass [a4paper, 12pt, oneside]{scrartcl}

\usepackage [cp1251] {inputenc}
\usepackage [english, russian] {babel}
\usepackage{indentfirst}
\usepackage{misccorr}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{ccaption}
\usepackage{upgreek}
\usepackage{wrapfig}
\usepackage{ifthen}

%command adds n textcommands;1st parameter - n; 2nd parameter - textcommand
\newcommand{\commandrep}[2]
{
 \newcounter{i}
 \setcounter{i}{0}
 \whiledo{\not\equal{\value{i}}{#1}}
  {
   \addtocounter{i}{1}
   #2 
  }
}
 
%leave n empty string;
\newcommand{\printent}[1]
{
 \commandrep{#1}{\newline}
}

\newcommand{\rv}{\text{В}}
\newcommand{\ra}{\text{А}}
\newcommand{\rma}{\text{мА}}
\newcommand{\rom}{\text{Ом}}
\newcommand{\rvt}{\text{Вт}}

\begin{document}
\begin{titlepage}
 \begin{center}
  texttexttext
  \printent{5}
  texttext
  \printent{7}  
  texttext 
  texttext
 \end{center}
\end{titlepage}
\end{document}
Last edited by artemff on Sun Nov 01, 2009 4:47 pm, edited 1 time in total.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Defining a command with counter

Post by gmedina »

Hi,

change

Code: Select all

\newcommand{\commandrep}[2]
{
\newcounter{i}
\setcounter{i}{0}
\whiledo{\not\equal{\value{i}}{#1}}
  {
   \addtocounter{i}{1}
   #2
  }
}
to

Code: Select all

\newcounter{i}
\newcommand{\commandrep}[2]
{
\setcounter{i}{0}
\whiledo{\not\equal{\value{i}}{#1}}
  {
   \addtocounter{i}{1}
   #2
  }
}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
artemff
Posts: 113
Joined: Sat Oct 17, 2009 11:15 pm

Defining a command with counter

Post by artemff »

gmedina, Thanks. But why next example with this function don't work?

Code: Select all

\documentclass [a4paper, 12pt, oneside]{scrartcl}

\usepackage [cp1251] {inputenc}
\usepackage [english, russian] {babel}
\usepackage{indentfirst}
\usepackage{misccorr}
\usepackage{ifthen}


\newcounter{i}
%command adds n textcommands;1st parameter - n; 2nd parameter - textcommand
\newcommand{\commandrep}[2]
{
 \setcounter{i}{0}
 \whiledo{\not\equal{\value{i}}{#1}}
  {
   \addtocounter{i}{1}
   #2 
  }
}
 
%leave n empty string;
\newcommand{\printent}[1]
{
 \commandrep{#1}{\newline}
}

\newcommand{\rv}{\text{В}}
\newcommand{\ra}{\text{А}}
\newcommand{\rma}{\text{мА}}
\newcommand{\rom}{\text{Ом}}
\newcommand{\rvt}{\text{Вт}}

\begin{document}
\begin{titlepage}
 \begin{center}
  texttexttexttext
  \printent{4}
  texttexttexttext
  \printent{5}  
  texttexttexttexttext
  \newline
  \begin{flushright}
   texttexttexttexttexttexttext
  \end{flushright}
  \printent{6}
  texttexttext
 \end{center}
\end{titlepage}
\end{document}
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Defining a command with counter

Post by gmedina »

It doesn't work because \printent internally uses the \newline command. The line \printint{6} in your code will result in using a \newline command outside a paragraph and this will produce an error.

Try changing

Code: Select all

\newcommand{\printent}[1]
{
\commandrep{#1}{\newline}
}
to

Code: Select all

\newcommand{\printent}[1]
{
\commandrep{#1}{\vskip\baselineskip}
}
In fact, you could define \printent in the following way:

Code: Select all

\newcommand{\printent}[1]%
{\vskip#1\baselineskip}
and now the \commandrep definition is not longer needed.
Last edited by gmedina on Sun Nov 01, 2009 5:20 pm, edited 2 times in total.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
artemff
Posts: 113
Joined: Sat Oct 17, 2009 11:15 pm

Re: My commands don't work.

Post by artemff »

gmedina, Thanks, now all work...
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Re: My commands don't work.

Post by gmedina »

I made a mistake in my previous reply. I corrected it.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
artemff
Posts: 113
Joined: Sat Oct 17, 2009 11:15 pm

Re: My commands don't work.

Post by artemff »

gmedina, Instead enters are numbers...
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Defining a command with counter

Post by gmedina »

artemff wrote:gmedina, Instead enters are numbers...
I do not undestand you here. What do you mean?
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Post Reply