Notebook

これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。

HTML ファイル生成日時: 2024/09/03 20:25:59.812 (台灣標準時)

LaTeX での counter の利用方法について

講義資料を作っているとき、講義資料の中に練習問題を含めたいことがあるで ござる。その際、それぞれの練習問題に番号をつけておくと便利でござる。講 義中に、「では、次に 10 個目の練習問題をやってみるでござる。」などと言 えば、どの問題に取り組めばよいのかわかりやすいからでござる。この番号を 手作業で一つ一つ文書中に書き込んでおくと、面倒なことが起きる場合がある でござる。例えば、練習番号の順番を入れ替えたり、途中に新しい練習問題を 加えたり、既にある練習問題を削除したりする場合に、多数の番号を書き直さ なくてはならないことになることがあるでござる。 LaTeX を使っているのな らば、練習問題の数を数えて、番号をふる、などということは、 LaTeX にや らせた方が確実で間違いがないでござるし、単純作業を LaTeX に任せる方が より複雑な作業に集中できてよいでござる。

LaTeX で counter を使うには、例えば、以下のようにすればよいようでござ る。 \newcounter{} で新しいカウンターを作成し、 \setcounter{} で初期値を設定し、 \stepcounter{} でカウンターの値を一つ増やし、 \theカウンター名 でそのとき のカウンターの値を出力できるでござる。 \newcommand{}{} を使っ て、 increment と値の出力をコマンド一つでできるようにしておくと、便利 なようでござる。


\documentclass{article}

\usepackage{tcolorbox}

\newcounter{ExerciseNo}
\setcounter{ExerciseNo}{0}
\newcommand{\PrintExerciseNo}{\stepcounter{ExerciseNo}\theExerciseNo}

\begin{document}

 \begin{tcolorbox}
  [title={\bf Exercise s08-\PrintExerciseNo}, 
  colframe=blue, colback=blue!15!white]
  Make a Python script to calculate $\sin (30^\circ)$ and print the
  result of calculation.
 \end{tcolorbox}

 \begin{tcolorbox}
  [title={\bf Exercise s08-\PrintExerciseNo}, 
  colframe=blue, colback=blue!15!white]
  Make a Python script to calculate $\cos (60^\circ)$ and print the
  result of calculation.
 \end{tcolorbox}

 \begin{tcolorbox}
  [title={\bf Exercise s08-\PrintExerciseNo}, 
  colframe=blue, colback=blue!15!white]
  Make a Python script to calculate $\tan (45^\circ)$ and print the
  result of calculation.
 \end{tcolorbox}

\end{document}

上の LaTeX ファイルを組版した結果は、以下の通りでござる。

fig_202311/test_counter_01.png

これで、基本的には問題ないのでござるが、一つ修正したいことは、常に番号 を二桁にし、 10 よりも小さな数字の場合には、左側にゼロを付け加える、と いうことでござる。どのようにすればよいのか調べてみたところ、 \two@digits{} を使えばよいようでござった。試してみたでござ る。カウンターの値を出力するには \theカウンター名 を使えば よいようなのでござるが、別のマクロによる処理にカウンターの値を使いたい 場合には、 \value{} を使うようでござる。また、 @ を含むマクロを使う場合には、 @ を含む部分を \makeatletter\makeatother で挟んでおく必要が あるようでござる。


\documentclass{article}

\usepackage{tcolorbox}

\newcounter{ExerciseNo}
\setcounter{ExerciseNo}{0}
\makeatletter
\newcommand{\PrintExerciseNo}
{\stepcounter{ExerciseNo}\two@digits{\value{ExerciseNo}}}
\makeatother

\begin{document}

 \begin{tcolorbox}
  [title={\bf Exercise s08-\PrintExerciseNo}, 
  colframe=blue, colback=blue!15!white]
  Make a Python script to calculate $\sin (30^\circ)$ and print the
  result of calculation.
 \end{tcolorbox}

 \begin{tcolorbox}
  [title={\bf Exercise s08-\PrintExerciseNo}, 
  colframe=blue, colback=blue!15!white]
  Make a Python script to calculate $\cos (60^\circ)$ and print the
  result of calculation.
 \end{tcolorbox}

 \begin{tcolorbox}
  [title={\bf Exercise s08-\PrintExerciseNo}, 
  colframe=blue, colback=blue!15!white]
  Make a Python script to calculate $\tan (45^\circ)$ and print the
  result of calculation.
 \end{tcolorbox}

\end{document}

上の LaTeX ファイルを組版した結果は、以下の通りでござる。

fig_202311/test_counter_02.png

参考文献

LaTeX の counter についての参考文献は以下の通りでござる。

LaTeX 全般についての有用そうな文書は以下の通りでござる。



Frequently accessed files

  1. Computer___Python/20220518_0.html
  2. Computer___Network/20230726_00.html
  3. Computer___Network/20230516_00.html
  4. Computer___FreeBSD/20220621_0.html
  5. Misc___Taiwan/20240207_00.html
  6. Computer___Python/20220715_0.html
  7. Food___Taiwan/20220429_0.html
  8. Computer___Network/20230508_00.html
  9. Computer___NetBSD/20220817_3.html
  10. Computer___Python/20220410_0.html
  11. Computer___Debian/20210223_1.html
  12. Computer___Python/20210124_0.html
  13. Computer___NetBSD/20220818_1.html
  14. Computer___NetBSD/20220428_0.html
  15. Computer___Python/20221013_0.html
  16. Science___Math/20220420_0.html
  17. Computer___NetBSD/20230119_00.html
  18. Science___Astronomy/20220503_0.html
  19. Computer___TeX/20230503_00.html
  20. Computer___NetBSD/20220808_0.html
  21. Computer___Network/20240130_00.html
  22. Computer___NetBSD/20230515_00.html
  23. Computer___Network/20240416_00.html
  24. Computer___NetBSD/20240101_02.html
  25. Travel___Taiwan/20220809_2.html
  26. Computer___NetBSD/20210204_0.html
  27. Computer___Python/20220816_1.html
  28. Science___Astronomy/20220420_1.html
  29. Food___Taiwan/20230526_00.html
  30. Computer___Python/20240101_00.html


HTML file generated by Kinoshita Daisuke.