3

I write the file out.csv, which contains umlauts and tried to read it in. How can I make this work?

\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}

cis
  • 8,073
  • 1
  • 16
  • 45

2 Answers2

3

You need to expand \word before applying \detokenize

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

enter image description here

There's no need to do \expandafter\detokenize\expandafter{\word}, because \detokenize expands tokens while looking for {.

For more complex applications, you might try \protected@iwrite, see https://tex.stackexchange.com/a/596328/4427

egreg
  • 1,121,712
  • What about using your \protected@iwrite from https://tex.stackexchange.com/a/542425/38080? Maybe it's more general... – Rmano May 28 '21 at 15:48
  • @Rmano That's indeed another good idea. – egreg May 28 '21 at 15:51
  • it really is a Ulrike's idea https://chat.stackexchange.com/transcript/message/58155687#58155687, I just went over to learn ;-) – Rmano May 28 '21 at 15:56
1

As suggested by Ulrike Fischer in chat, another possibility is to use @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}

enter image description here

Rmano
  • 40,848
  • 3
  • 64
  • 125
  • Yes, looking good; should work with that. My original code does not work further with that (same error). Maybe my fault, I have to look. – cis May 28 '21 at 19:15
  • Can I ask you again here: https://tex.stackexchange.com/q/599036/46023 – cis May 29 '21 at 04:17