如何创建一个问题环境,其中问题编号位于左边距并且解决方案和问题文本对齐?

如何创建一个问题环境,其中问题编号位于左边距并且解决方案和问题文本对齐?

我正在尝试创建一个用于提问和解决问题的环境。我的代码是:

\documentclass{article}
\usepackage{mathtools}
\newcounter{problema}[section]
\newenvironment{problema}[2]
    {\noindent\hspace{-25pt}\makebox[20pt][r]{\refstepcounter{problema}\textbf{\theproblema}}
        \makebox[5pt][l]{}\textbf{\textit{#1}} 
        \newline #2\par\medskip
        \noindent\(\looparrowright\)
    }
    {\bigskip}

\begin{document}
    \begin{problema}{The topic of the problem}
        {The text of the problem}
        The solution
    \end{problema}
\end{document}

我实现的结果是: 在此处输入图片描述

如何才能让“问题主题”、“问题文本”和“解决方案”之前的符号对齐(不缩进)?

编辑:我用过\documentclass{article}

答案1

这是一个带有边缘数字的解决方案:

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{mathtools, amssymb}
\newcounter{problema}[section]
\newenvironment{problema}[2]
    {\noindent\llap{\makebox[20pt][r]{\refstepcounter{problema}\textbf{\theproblema}}\hskip5pt}\hspace*{-0.4\fontdimen2\font}%
    \textbf{\textit{#1}}
        %\makebox[5pt][l]{}\textbf{\textit{#1}}
        \newline #2\par\medskip
        \noindent\(\looparrowright\)
    }
    {\bigskip}

\begin{document}

    \begin{problema}{The topic of the problem }
        {The text of the problem.The text of the problem. The text of the problem.The text of the problem. The text of the problem.The text of the problem. The text of the problem.The text of the problem. }
        The solution. The solution. The solution. The solution. The solution. The solution. The solution. The solution. The solution. The solution. The solution. The solution. The solution. The solution. The solution. The solution.
    \end{problema}

\end{document}

在此处输入图片描述

答案2

我假设您使用文章文档类。

这是一个有效的代码。

\documentclass{article}
%\usepackage{mathtools} %<--------- why you added this?
\usepackage{amssymb}%<---- I added this

\newcounter{problema}[section]
\newenvironment{problema}[2]
{
\noindent   \refstepcounter{problema}\textbf{\theproblema}
    \noindent \textbf{\textit{#1}}
    \newline #2\par\medskip
    \noindent\(\looparrowright\)
}
{\bigskip}
\begin{document}
    \begin{problema}{The topic of the problem}
        {The text of the problem The text of the problem The text of the problem The text of the problem The text of the problem The text of the problem The text of the problem The text of the problem The text of the problem The text of the problem }
        The solution The solution The solution The solution The solution The solution The solution The solution The solution The solution 
    \end{problema}
\end{document}

输出: 在此处输入图片描述

不过,我觉得它看起来不太好。以下是我建议你使用的。

\documentclass{article}
%\usepackage{mathtools} %<--------- why you added this?
\usepackage{amssymb}%<---- I added this
\newcommand{\addindentation}{\par
    \leftskip=2em
    \noindent\ignorespaces}{%
    \par\medskip}
\newcounter{problema}[section]
\newenvironment{problema}[2]
{\refstepcounter{problema}
\noindent   \textbf{Problem \theproblema. \textit{#1}}
\addindentation
    \newline #2\par\medskip
    \noindent\(\looparrowright\) \textbf{Solution.}
}
{\bigskip}
\begin{document}
    \begin{problema}{The topic of the problem}
        {The text of the problem The text of the problem The text of the problem The text of the problem The text of the problem The text of the problem The text of the problem The text of the problem The text of the problem The text of the problem }
        The solution The solution The solution The solution The solution The solution The solution The solution The solution The solution 
    \end{problema}

    \begin{problema}{The topic of the problem}
    {The text of the problem The text of the problem The text of the problem The text of the problem The text of the problem The text of the problem The text of the problem The text of the problem The text of the problem The text of the problem }
    The solution The solution The solution The solution The solution The solution The solution The solution The solution The solution 
\end{problema}
\end{document}

在此处输入图片描述

我还建议您检查一下包tcolorbox,使用它您可以获得更好的结果。

相关内容