用变音符号进行 newwrite 和 read

用变音符号进行 newwrite 和 read

我编写了包含变音符号的文件out.csv并尝试读入它。我该如何让它工作?

\documentclass[margin=5pt, varwidth]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\begin{document}
\newwrite\out
\immediate\openout\out=out.csv
\foreach[count=\n from 0] \word in {ä, ö, ü, ß, x, y}{
\immediate\write\out{\n; \word }% works not
%\immediate\write\out{\n; \detokenize{\word} }% works not too
}
\immediate\closeout\out
\input{out.csv}

\pgfplotstableread[col sep=semicolon, header=false]{out.csv}{\mytable}
\pgfplotstabletypeset[string type]{\mytable}
\end{document}

答案1

根据建议Ulrike Fischer 在聊天中,另一种可能性是使用@egreg\protected@iwrite

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\makeatletter
\long\def\protected@iwrite#1#2#3{%
  \begingroup
  #2%
  \let\protect\@unexpandable@protect
  \edef\reserved@a{\immediate\write#1{#3}}%
  \reserved@a
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi
}
\newcommand{\mywrite}[2]{\protected@iwrite#1{}{#2}}%

\makeatother
\begin{document}
\newwrite\out
\immediate\openout\out=out.csv
\foreach[count=\n from 0] \word in {ä, ö, ü, ß, x, y}{
    \mywrite\out{\n; \word ; \word}% now it works
}
\immediate\closeout\out
\input{out.csv}

\pgfplotstableread[col sep=semicolon, header=false]{out.csv}{\mytable}
\pgfplotstabletypeset[string type]{\mytable}
\end{document}

在此处输入图片描述

答案2

\word申请之前你需要扩展\detokenize

\immediate\write\out{\n: \detokenize\expandafter{\word}}

在此处输入图片描述

没有必要执行\expandafter\detokenize\expandafter{\word},因为\detokenize在寻找时会扩展标记{

对于更复杂的应用程序,你可以尝试\protected@iwrite查看https://tex.stackexchange.com/a/596328/4427

相关内容