2016-08-02 6 views
0

私はlatexにはかなり新しく、できるだけラテックスを少なく書くような質問をするためのフォーマットを作成しようとしています。パラメータを使用して環境や環境にコマンドを埋め込む

\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

は、考慮すべきいくつかのものです:コマンドは\newcommand{\somecmd}のように、その制御シーケンスで定義されている間

  1. 環境は、\newenvironment{someenv}のように、名前で定義されています。 \に注意してください。これがコードの主な問題です。

  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} 
関連する問題