\newenvironment 中の filecontents のファイル名の指定
以下で \input{\filename} を入れなければコンパイルはできますが、当然なにも表示されません。
\input{\filename} を入れると、ファイルは作成されるものの、コンパイルができません。
(TeXLive 2019 の pdflatex を使用しています。)
より具体的には、
./output/q-hoge-fuga.tex が無い状態で実行すると、
LaTeX Warning: Writing file `././output/q-hoge-fuga.tex'.
(./output/q-hoge-fuga.tex
! Missing number, treated as zero.
<to be read again>
\protect
l.1 %% LaTeX2e file `./output/q-hoge-fuga.tex'
となります。
この状態でもう一度、pdflatex を実行すると、
LaTeX Warning: File `./output/q-hoge-fuga.tex' already exists on the system.
Not generating it from this source.
(./output/q-hoge-fuga.tex (./output/q-hoge-fuga.tex (./output/q-hoge-fuga.tex
(./output/q-hoge-fuga.tex (./output/q-hoge-fuga.tex (./output/q-hoge-fuga.tex
(./output/q-hoge-fuga.tex (./output/q-hoge-fuga.tex (./output/q-hoge-fuga.tex
(./output/q-hoge-fuga.tex (./output/q-hoge-fuga.tex (./output/q-hoge-fuga.tex
(./output/q-hoge-fuga.tex (./output/q-hoge-fuga.tex
! TeX capacity exceeded, sorry [text input levels=15].
<argument> "./output/q-hoge-fuga.tex"
l.1 %% LaTeX2e file `./output/q-hoge-fuga.tex'
となります。 (再帰的に読み込もうとしている?)
どのようにすれば意図通りの動作になりますでしょうか?
\documentclass{article}
\usepackage{xstring}
\begin{document}
\def\outdir{./output/}
\def\mkfilename#1{
\StrSubstitute{#1}{:}{-}[\filename]
\edef\filename{\outdir\filename.tex}
% 結果を変数に格納する
% (できれば文字列を返し、\newenvironment の中で、\def\filename{\mkfilename{#1}} とかしたい)
}
% 第一引数は xx:yy:zz のような形式のラベル。
% xx 等には特殊な文字や空白などは入らないと仮定。
% この文字列中の : を - に変えたファイル名でファイル作成したい。
% (変更前の文字列は、tcolorbox でラベルを付けるために使用)
\newenvironment{question}[2]{%
\mkfilename{#1}
\csname filecontents\endcsname{\filename}%
%
% \begin{filecontents}{\filename}%
% だと上手くいかない
%
% 後でここに \begin{tcolorbox} を追加したい
% (#2 は tcolorbox 用)
}{% 後でここに \end{tcolorbox} を追加したい
%
% \end{filecontents} % だと上手くいかない
\csname endfilecontents\endcsname%
\input{\filename}% (*1) これが上手くいかない <-----
}
% ./output/q-hoge-fuga.tex としてファイルを作成したい
\begin{question}{q:hoge:fuga}{Title}
$E = mc^{2}$
\end{question}
\end{document}
追加の質問 (こちらは無視していただいても結構です):
上記コードで \begin{filecontents}{\filename}, \end{filecontents} だと以下のようなエラーが出るのですが、
なぜなのでしょう?
(上のコードは検索で出てきたものを参考に書きました)。
LaTeX Warning: Writing file `././output/q-hoge-fuga.tex'.
)
Runaway argument?
! File ended while scanning use of ^^M.
<inserted text>
\par
<*> q.tex