我创建了一个包含 2 个 foreach 循环的表。现在我想将其也作为文本文件。
但是有一些 if-decision。newwrite
这里如何正确使用?
\documentclass[]{article}
\usepackage{tikz}
\begin{document}
\def\xHead{A,I,U,E,O}
\def\yHead{xx, K,S,T,N,H,M,Y,R,W}
\section{In the Document - good}
% Headrow
yy, --A, --I, --U, --E, --O, N
\foreach[count=\row] \y in \yHead{%
\ifnum\row=1 \y, \else \y-- \fi%
\ifnum\row=1 \xHead, N \else
\foreach \x in \xHead{%
\y\x, } nn \fi
\par}
\section{As writeout - bad}
\newwrite\kanaoutfile
\immediate\openout\kanaoutfile=KanaData.txt%
% Headrow
\immediate\write\kanaoutfile{yy, --A, --I, --U, --E, --O, N}
\foreach[count=\row] \y in \yHead{%
\immediate\write\kanaoutfile{\ifnum\row=1 \y, \else \y-- \fi}%
\ifnum\row=1 \immediate\write\kanaoutfile{\xHead, N} \else%
\foreach \x in \xHead{%
\immediate\write\kanaoutfile{\y\x,} nn } \fi%
\par}
\immediate\closeout\kanaoutfile
\subsection{Test}
\input{KanaData.txt}
\end{document}
答案1
使用 pgfmath:
\documentclass[]{article}
\usepackage{tikz}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\begin{document}
\section{In the Document and as outfile - good}
\newwrite\kanaoutfile
\immediate\openout\kanaoutfile=KanaData.txt%
\newcommand{\row}{}% reserv Name
\foreach[count=\y from 0] \Y in {{}, {}, K,S,T,N,H,M,Y,R,W}{%%%
\let\row=\empty% create List
\foreach[count=\x from 0] \X in {{}, A,I,U,E,O,N}{%%
\pgfmathparse{
\y==0 ? (\x==0 ? "yy" : (\x==6 ? "\X" : "--\X")) :
(\y==1 ? (\x==0 ? "ee" : "\X") : (\x==0 ? "\Y--" :
(\x==6 ? "nn" : "\Y\X")))}%
\ifx\empty\row{} \xdef\row{\pgfmathresult}%
\else \xdef\row{\row,\pgfmathresult}%
\fi
}%%
\noindent\row \\
\immediate\write\kanaoutfile{\row}
}%%5
\immediate\closeout\kanaoutfile
\subsection{Test}
%Show: \row
%\input{KanaData.txt}
\pgfplotstabletypeset[col sep=comma, string type]{KanaData.txt}
\end{document}