2016-08-02 62 views
0

我很新的乳膠,我試圖創建一個格式的考試問題,將盡可能少寫乳膠。 目前我寫了這個代碼:嵌入命令到參數環境和環境

\documentclass{article} 

%the question environment wrapping every exam questions 
\newenvironment{q}[2] { 
    \newcounter{answerCounter} %used for display of answer number with question 
    \setcounter{answerCounter}{0} 
    \newcommand{a}[1] { 
      \item a\value{answerCounter}: ##1 
      %I used double hyphen on previous line because i'm within an environment 
      \addtocounter{answerCounter}{1} 
    } 

    \item q#1: #2 
    %the 1st param of q (the environment) is the question number, 2nd is the question itself 
    \begin{itemize} 
} { \end{itemize} } 

\begin{document} 

\begin{itemize} 
    \begin{q}{1}{to be or not to be?} 
      \a{to be} 
      \a{not to be} 
    \end{q} 
    \begin{q}{2}{are you john doe?:} 
      \a{No i'm Chuck Norris} 
      \a{maybe} 
      \a{yes} 
    \end{q} 
\end{itemize} 

\end{document} 

,我希望它顯示這樣:

enter image description here

但是當我做pdflatex exam.tex我得到以下第2個錯誤(有更多的,但我不想讓你知道信息):

! Missing control sequence inserted. 
<inserted text> 
       \inaccessible 
l.21   \begin{q}{1}{to be or not to be?} 

? 
(/usr/share/texlive/texmf-dist/tex/latex/base/omscmr.fd) 

! LaTeX Error: Command \to be unavailable in encoding OT1. 

See the LaTeX manual or LaTeX Companion for explanation. 
Type H <return> for immediate help. 
...            

l.22     \a{to be} 

? 

我有沒有調用/定義我的環境和命令錯?謝謝。

回答

1

這裏有一些事情要考慮:

  1. 環境與名稱定義,如\newenvironment{someenv},同時命令與他們的控制序列定義,如\newcommand{\somecmd}。請注意0​​。這是你的代碼的主要問題。

  2. LaTeX定義了許多單字符控制序列,通常用於符號上的重音。在對上面(1)的示例進行更正後,已經定義了\a。相反,定義更具描述性的內容以增加代碼的可讀性。

  3. 您可能會在代碼中插入一些僞造空格。這些是通過%的戰略佈局來阻止的。請參閱What is the use of percent signs (%) at the end of lines?

  4. 在其他命令(如新計數器)中定義命令可能會導致問題,或使事情不必要地變慢(在較大的文檔和使用情況下)。而是定義外部的環境 - 在全球範圍內 - 然後根據需要重置該數字。

enter image description here

\documentclass{article} 

\newcounter{answerCounter} %used for display of answer number with question 
%the question environment wrapping every exam questions 
\newenvironment{question}[2] {% 
    \setcounter{answerCounter}{0}% 
    \newcommand{\ans}[1]{% 
    \stepcounter{answerCounter}% 
    \item a\theanswerCounter: ##1 
    %I used double hyphen on previous line because i'm within an environment 
    } 

    \item q#1: #2 
    %the 1st param of q (the environment) is the question number, 2nd is the question itself 
    \begin{itemize} 
} { \end{itemize} } 

\begin{document} 

\begin{itemize} 
    \begin{question}{1}{To be or not to be?} 
    \ans{to be} 
    \ans{not to be} 
    \end{question} 
    \begin{question}{2}{Are you John Doe?} 
    \ans{No I'm Chuck Norris} 
    \ans{maybe} 
    \ans{yes} 
    \end{question} 
\end{itemize} 

\end{document}