我想得到以下內容:
為此,我使用了以下程式碼:
\documentclass{book}
\usepackage{enumerate}
\begin{document}
\begin{enumerate}
\item First item
\item Second item
\end{enumerate}
[Hint:
\begin{enumerate}
\item Hint for first item
\item Hint for Second item
\end{enumerate}]
\end{document}
產生:
我怎樣才能得到我想要的確切結果?
答案1
我不知道你是否願意尋求建議,但這裡有一個:
\documentclass{article}
\newcounter{hintcntr}
%\renewcommand{\thehintcntr}{\arabic{hintcntr}}
\makeatletter
\newenvironment{hints}
{% \begin{hints}
\setcounter{hintcntr}{0}% Restart numbering
\renewcommand{\item}{\stepcounter{hintcntr}\@ifstar\@itemstar\@itemnostar}
\def\@itemstar{\ignorespaces}% \item*
\def\@itemnostar{\ignorespaces\thehintcntr.~}% \item
\par%
[~Hint:%
}
{\unskip~]}% \end{hints}
\makeatother
\begin{document}
\begin{enumerate}
\item First item
\item Second item
\item Third item
\item Last item
\end{enumerate}
\begin{hints}
\item Hint for first item in list
\item Hint for second item in list
\item*% no hint here
\item Hint for last item in list
\end{hints}
\end{document}
環境hints
設定一個常規段落並\item
列印下一個提示,而\item*
僅步進提示計數器而不列印任何內容。
hints
以下是與您目前請求的輸出相符的環境的實作:
\newcounter{hintcntr}
%\renewcommand{\thehintcntr}{\arabic{hintcntr}}
\makeatletter
\newenvironment{hints}
{% \begin{hints}
\setcounter{hintcntr}{0}% Restart numbering
\def\newpar{\def\newpar{\par}}% http://tex.stackexchange.com/a/89187/5764
\renewcommand{\item}{\stepcounter{hintcntr}\@ifstar\@itemstar\@itemnostar}
\def\@itemstar{\ignorespaces}% \item*
\def\@itemnostar{\ignorespaces\newpar\noindent\thehintcntr.~}% \item
\par\noindent%
[~Hint:%
}
{\unskip~]}% \end{hints}
\makeatother
答案2
另一個選擇是使用enumitem
包含其inline
選項和enumerate*
內部清單的套件。可以輕鬆設定新清單以產生所需的輸出:
\documentclass{book}
\usepackage[inline]{enumitem}
\newlist{hint}{enumerate*}{1}
\setlist[hint,1]{label=\arabic*.}
\newenvironment{hints}
{[Hint:~\begin{hint}}
{\end{hint}]}
\begin{document}
\begin{enumerate}
\item First item
\item Second item
\end{enumerate}
\begin{hints}
\item Hint for first item \\
\item Hint for Second item
\end{hints}
\end{document}