0
2

More than 1 year has passed since last update.

Latexの出力内容を切り替える

Last updated at Posted at 2023-08-08

動機

高校生向けに教材作成をしていたところ、生徒用(問題のみ表示)とカンペ用(解答も含めて表示)の切り替えがしたいなぁ〜と思ったので。

方法①:プリアンプルで一括設定する

commentパッケージの\excludecommentコマンド
xparseパッケージの\NewDocumentEnvironmentコマンド

プリアンプル
    \usepackage{comment}
    \usepackage{xparse}

    \NewDocumentEnvironment{outputstyle1}{O{}}{}{}
    \NewDocumentEnvironment{outputstyle2}{O{}}{}{}

    % \excludecomment{outputstyle1} %%表示させたい場合はコメントアウト
    \excludecomment{outputstyle2} %%表示させたい場合はコメントアウト

上記の場合、outputstyle2環境の内容が非表示となる。
outputstyle1環境の内容は普通に表示される。)

方法②:本文中で切り替える

Boolean変数\answershowを定義して\ifによる条件分岐を行う。
\answershowtrue \answershowfalseによって本文中で変数を設定し直すことが可能。

プリアンプル
    \NewDocumentEnvironment{answer}{O{}}{}{}
    \newif\ifanswershow
    % \answershowtrue % 非表示にさせたい場合コメントアウト
    \answershowfalse  % 表示させたい場合コメントアウト
    \ifanswershow \else
    \excludecomment{answer}
    \fi

本文
    \answershowtrue %% 以下answer環境が表示される
    %% 例題
    \begin{enumerate}[(1)]
    \item $x^2+2x-3=0$を解け。
    \item $2x^2-x+1=0$を解け。
    \end{enumerate}
    \begin{answer}
    %% ここに解答を記入 %%
    \end{answer}
    \answershowfalse %% 以下answer環境が非表示となる
    %% 練習問題

使用例

問題文をquestion#.texファイルに記述し、answer環境に解答を記述。

本文

\begin{outputstyle1}
    \section{演習問題}
    \answershowfalse
    \subsection{微分と不等式}
    \begin{enumerate}
    \item \input{question1.tex}
    \item \input{question2.tex}
    \item \input{question3.tex}
    \end{enumerate}
\end{outputstyle1}

\begin{outputstyle2}
    \section{演習問題}
    \answershowtrue
    \subsection{微分と不等式}
    \begin{enumerate}
    \item \input{question1.tex}
    \item \input{question2.tex}
    \item \input{question3.tex}
    \end{enumerate}
\end{outputstyle2}
question#.tex
    \begin{enumerate}[(1)]
    \item #問題文
        \begin{answer}
        #解答
        \end{answer}
    \item #問題文
        \begin{answer}
        #解答
        \end{answer}
    \end{enumerate}

レイアウトにこだわる場合には調整などを行う手間は結局変わらないが、ファイルを2つ用意するのが面倒な時には便利。そもそも問題をファイル管理する必要はあり。

ダミー環境を用意して\excludecomment{環境名}で隠すという発想でした。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2