整个自定义环境的缩进是否相同?

整个自定义环境的缩进是否相同?

我定义了一个如下所示的环境:

\newcounter{question}
\newenvironment{question}[2][]{\refstepcounter{question}\noindent \textbf{Question~\thequestion: #1} #2\\}

在我的乳胶文档中我这样使用它:

\question{What is the difference between System.gc() and Runtime.gc()?}
\question{If an object reference is set to null, will the Garbage Collector immediately free the memory held by that object?}

这将生成以下输出:

在此处输入图片描述

但是,我希望问题 7 中第二行的单词“held”与单词 if 具有相同的缩进。我该如何实现?

答案1

一种方法是使用一个list环境来\item处理每个问题。并且不重置计数器。

\documentclass{article}
\usepackage{lipsum}
\usepackage{calc}
\newcounter{question}
\newcommand\MyQuestionList[1]{%
  \begin{list}{%
      \refstepcounter{question}%
      \textbf{Question \thequestion:}%
    }{%
      \settowidth{\labelwidth}{\textbf{Question \thequestion:}}
      \setlength{\leftmargin}{\labelwidth+\labelsep}%
    }
  \item #1
  \end{list}%
}

\begin{document}
\lipsum[1]

\MyQuestionList{\lipsum[2]}

\MyQuestionList{New qestion}

Set the question couter to 25, i.e. the next question is 26.
\setcounter{question}{25}

\MyQuestionList{\lipsum[3]}

\lipsum[4]
\end{document}

我已将计数器设置为 25,只是为了显示缩进遵循每个问题的标签。

在此处输入图片描述

答案2

enumerate-like使用 定义新环境更加容易enumitem。对于连续编号,请使用resume以下选项enumitem

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{enumitem}

\newlist{questions}{enumerate}{1}
\setlist[questions]{label=\bfseries Question \arabic*: , align=left, widest=\textbf{999}, leftmargin=*, ref=\arabic*}
\def\qu{\item}

\begin{document}

\begin{questions}%
    \qu What is the difference between System.gc() and Runtime.gc()?\label{q-1}
    \qu If an object reference is set to null, will the Garbage Collector immediately free the memory held by that object?
    \setcounter{questionsi}{99}
    \qu Some test text. Some test text. Some test text. Some test text. Some test text. 
\end{questions}
Proof of \ref{q-1}. —
\begin{questions}[resume]
    \qu Some test text. Some test text. Some test text. Some test text. Some test text.
\end{questions}

\end{document} 

在此处输入图片描述

您还可以根据计数器的值(1 到 9 之间,或 10 到 99 之间,等等)设置变量列表左边距:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{enumitem}

\newlist{questions}{enumerate}{1}
\setlist[questions]{label=\bfseries Question \arabic*: , align=left, leftmargin=*, ref=\arabic*}
\def\qu{\item}

\begin{document}

\begin{questions}%
    \qu What is the difference between System.gc() and Runtime.gc()?\label{q-1}
    \qu If an object reference is set to null, will the Garbage Collector immediately free the memory held by that object?
    \setcounter{questionsi}{97}
\end{questions}
Proof of \ref{q-1}. —
\begin{questions}[resume, widest=\textbf{99}, ]%
    \qu Some test text. Some test text. Some test text. Some test text. Some test text.
    \qu Another test text. Another test text. Another test text. Another test text. Another test text.
\end{questions}
\begin{questions}[resume, widest=\textbf{999}, ]
    \qu Some test text. Some test text. Some test text. Some test text. Some test text.
    \qu Another test text. Another test text. Another test text. Another test text. Another test text.
\end{questions}

\end{document} 

在此处输入图片描述

答案3

这只是另一个没有列表的解决方案(实际上有两个解决方案,一个有\hangindent,一个没有):

\documentclass[a4paper]{article}

\newcounter{question}
\newcommand{\qsparbox}[2][\relax]{%
    \refstepcounter{question}%
    \ifx#1\relax%
        \setbox0\hbox{\textbf{Question~\thequestion:}}%
    \else%
        \setbox0\hbox{\textbf{Question~\thequestion: #1}}%
    \fi%
    \noindent\usebox0\hskip1em\parbox[t]{\dimexpr\textwidth-1em-\wd0}{#2}\\%
}
\newcommand{\qshangindent}[2][\relax]{%
    \refstepcounter{question}%
    \ifx#1\relax%
        \setbox0\hbox{\textbf{Question~\thequestion:}}%
    \else%
        \setbox0\hbox{\textbf{Question~\thequestion: #1}}%
    \fi%
    \par\hangindent3em%
    \noindent\usebox0\hskip1em#2\par%
}


\begin{document}
Using \verb|\parbox|:

\qsparbox{If an object reference is set to null, will the Garbage Collector immediately free the memory held by that object?}

\qsparbox[test]{If an object reference is set to null, will the Garbage Collector immediately free the memory held by that object?}

\vspace{1em}
Using \verb|\hangindent|:
\qshangindent{If an object reference is set to null, will the Garbage Collector immediately free the memory held by that object?}

\qshangindent[test]{If an object reference is set to null, will the Garbage Collector immediately free the memory held by that object?}
\end{document}

结果

相关内容