使用 `\ensuremath` 后的段落缩进

使用 `\ensuremath` 后的段落缩进

我创建了一个宏,用于写入试卷中练习的渐进编号。该宏在正常模式和数学模式下均可运行,因此我可以编写如下内容:

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

问题是:即使我设置了,每个段落的开头总会有缩进\parindent=0pt

这是我正在使用的代码:

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

如何删除练习编号前的段落缩进?

[编辑]

这是一个有效的例子:

\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}

答案1

第二次编辑(使用您的工作示例):

\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 %
\quad\quad\textbf{\alph{SubExersize}.\;}\else\textbf{\alph{SubExersize}.\;}\fi\ensuremath{\mathbf{#2}}%
\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}

输出:

在此处输入图片描述

编辑:

\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}

输出:

在此处输入图片描述

旧答案:(没有真正理解命令) 借助\noindent并添加了一些“%”:

\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}

“%”符号不允许在执行命令期间创建空格(搜索“latex tokens”)

该命令刚刚从命令中添加出来,因为如果命令在里面(如您的第二个示例所示),\noindent它就没有意义。\[\]

此外,您必须将命令的参数括在里面,{}以便清楚该命令作为输入的参数是什么。

输出:

在此处输入图片描述

相关内容