# Paragraph indentation after using \ensuremath

I have created a macro that writes the progressive number of the exercises in an exam sheet. The macros works in normal mode and in math mode, so that I am able to write something like this:

\esercizio How much is $1+1$?
Solve the following equation:
$\esercizio x-1=0$


The problem is: there is always an indentation at the beginning of each paragraph, even if I set \parindent=0pt.

This is the code I'm using:

\newcounter{NumEsercizio}
\newcommand{\esercizio}[1][]{
\stepcounter{NumEsercizio}
\ensuremath{\mathbf{\theNumEsercizio}.
\ifthenelse{\equal{#1}{}}
{}
{\text{ [#1]}}}
}


How can I remove paragraph indentation before the exercise number?

This is a working example:

\documentclass[a4paper,12pt]{article}
\usepackage[italian]{babel}
\usepackage{amsmath}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\pagestyle{empty}
\usepackage{lmodern}
\usepackage{ifthen}
\usepackage{parskip}

\parindent=0pt

\newcounter{NumEsercizio}
\newcommand{\esercizio}[1][]{%
\stepcounter{NumEsercizio}%
\ensuremath{\mathbf{\theNumEsercizio}.
\ifthenelse{\equal{#1}{}}
{}
{\text{ [#1]}}}
}

\begin{document}
\noindent
Risolvi le seguenti disequazioni:
$\esercizio\quad\frac{(x^2-1)(x-2)^2}{x^2+2}\le0,\qquad \esercizio\quad x^2(16-x^2)\le0.$

\noindent
\esercizio Risolvi il seguente sistema col metodo di riduzione,
poi rappresenta graficamente la soluzione:
$\begin{cases} 5x+2y=1\\ 5x-y=-8 \end{cases}$
\end{document}

• I gave a suggestion for what I suppose you want to achieve. – koleygr Oct 13 '18 at 22:30

Second Edit (with your working example):

\documentclass{article}
\usepackage{lipsum}
\usepackage{amsmath}
\newcounter{NumExersize}
\newcounter{SubExersize}
% #1 is the depth inside the exersize
% #2 is the content of the exercise item
\newcommand{\esercizio}[2][1]{%
\ifnum#1=1 %
\setcounter{SubExersize}{0}%
\stepcounter{NumExersize}%
\arabic{NumExersize}. {\bfseries #2}
\else
\stepcounter{SubExersize}%
\ifnum\value{SubExersize}>1 %
\fi
}
\begin{document}
\noindent
\esercizio{Risolvi le seguenti disequazioni:}
$\esercizio[2]{\frac{(x^2-1)(x-2)^2}{x^2+2}\le0}\quad \esercizio[2]{x^2(16-x^2)\le0.}$

\noindent
\esercizio{Risolvi il seguente sistema col metodo di riduzione,
poi rappresenta graficamente la soluzione:}
$\begin{cases} 5x+2y=1\\ 5x-y=-8 \end{cases}$
\end{document}


Output:

Edit:

\documentclass{article}
\usepackage{lipsum}
\newcounter{NumEsercizio}
\newcommand{\esercizio}[1][]{%
\def\arg{#1}%
\stepcounter{NumEsercizio}%
\ensuremath{\mathbf{\theNumEsercizio}}.{\ifx\arg\empty \else{ \bfseries #1}\fi}%
}
\begin{document}
\lipsum[1]

\noindent\esercizio[How much is $1+1$?]

Solve the following equations:
$\esercizio x-1=0$
\end{document}


Output:

Old answer: (without realy understanding the command) Whith the help of \noindent and some"%" added:

\documentclass{article}
\usepackage{ifthen}
\usepackage{lipsum}
\newcounter{NumEsercizio}
\newcommand{\esercizio}[1][]{%
\stepcounter{NumEsercizio}%
\ensuremath{\mathbf{\theNumEsercizio}.
\ifthenelse{\equal{#1}{}}
{}
{\text{ [#1]}}}
}
\begin{document}
\lipsum[1]

\noindent\esercizio{How much is $1+1$?}
Solve the following equation:
$\esercizio{x-1=0}$
\end{document}


The "%" symbol doesn't allow an empty space to be created during the execution of the command (search about "latex tokens")

The \noindent command just added out of the command because it doesn't make sense if the command is inside  as in your second example.

Also, you have to enclose the command's arguments inside {} to meke clear what is the argument that the command takes as input.

Output:

• It is just a best practice when you have an argument in a command, to make clear what is supposed to be the argument of it... May be in your case doesn't needed... but I don't even know if you would like to give to your command a whole paragraph or a whole line or what. This way every possible editor of the document will have a more clear view of your code.. But may be in your case doesn't needed. – koleygr Oct 13 '18 at 21:03
• By mistake I used a wrong photo that shows the command with one space (I was ready to show you why you have the problem with the tokens but finally didn't) – koleygr Oct 13 '18 at 21:05
• As @DavidCarlisle noticed, your command has just an optional argument, and so, my code has these extra {} that doesn't really do something instead of grouping the included text...(your command expects of just a possible optional argyment that would be enclosed inside [] and not {}) I don't really see the reason for this optional argument, and I suppose you could add (edit) in your post an example with the output you expect in some cases (At least one with an optional argument and one without) – koleygr Oct 13 '18 at 21:31
• @zar it's your document, but I don't think this helps your readers at all. (but +1 for this answer for doing what you ask anyway:-) – David Carlisle Oct 13 '18 at 21:39
• zar: I just edited to what I supposed you meant... But as @DavidCarlisle mentioned , you will find a better answer and ideas after editing – koleygr Oct 13 '18 at 21:44