I intend to print several plots by using pgfplots
and a CSV file with several columns, one for each plot. this is how it would be implemented for a single column:
\documentclass[12pt]{article}
% lua
\usepackage{luacode}
% Maths
\usepackage{amsmath, amssymb}
% Tikz
\usepackage{tikz}
\usetikzlibrary{datavisualization}
% pgf
\usepackage{pgfmath} % calculations
\usepackage{pgfplots}
\pgfplotsset{
compat=1.18,
width=\textwidth%
}
% Chem
\usepackage{chemgreek}
\usepackage{chemformula} % formulas quimicas
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
height= 9cm
}
\begin{axis}
[
title= {\ch{column 1}},
xlabel= {\chemlambda Wavelenght (nm)},
ylabel= {Molar Absorptivity},
xmin= 350, xmax= 850,
ymin= 0,
]
\addplot%
[white]
table[
col sep=comma,
header=true,
x index=0,
y index=1,
] { file.csv };
\end{axis}
\end{tikzpicture}
\end{document}
for the file you can simply write:
a,b,c,d
1,1,2,3
2,1,4,9
3,1,8,27
4,1,24,81
in a TXT file and rename as CSV.
So far i managed to rewrite using luacode*
like follows:
...
\begin{document}
\begin{luacode*}
chem = {
"\\ch{column 1}",
"\\ch{column 2}",
"\\ch{column 3}"
}
for i=1,3,1
do
tex.sprint(
[[
\begin{tikzpicture}
\pgfplotsset{height=9cm}
\begin{axis}[
]],
'title=', chem[1], ',\n',
[[
xlabel= {\chemlambda Wavelenght (nm)},
ylabel= {Molar Absorptivity},
xmin= 350, xmax= 850,
ymin= 0,
]
\addplot[white]
table[
col sep=comma,
header=true,
x index=0,
]],
' y index=', 1, "\n",
[[
] {file.csv};
\end{axis}
\end{tikzpicture}
]]
)
end
\end{luacode*}
\end{document}
- For some reason even though im using
luacode*
environment which should interpret\
as\
, that is not happening in single line strings, so i had to add\\
instead; - The loop will index from 1 to 3 and change
'y index=', 1
to'y index=', index
and'title=', chem[1]
to'title=', chem[index]
Problem is that it doesn't work, i keep receiving the following error message:
Package pgfplots Error: Sorry, the supplied plot command is unknown or unsupported by pgfplots! Ignoring it..
Which implies that lua is printing something wrong, so i decided to simply print
as instead of tex.sprint
, and got the following:
\begin{tikzpicture}
\pgfplotsset{height=9cm}
\begin{axis}[
title= \ch{column 1} ,
xlabel= {\chemlambda Wavelenght (nm)},
ylabel= {Molar Absorptivity},
xmin= 350, xmax= 850,
ymin= 0,
]
\addplot[white]
table[
col sep=comma,
header=true,
x index=0,
y index= 1
] {file.csv};
\end{axis}
\end{tikzpicture}
...
Notice how comparing to the original code, every time the string block breaks something happens and that space is added breaking the code somehow.
Things i've tried:
- Writing a single
tex.sprint
for every string block trying to fix it, but it seems to make it worse, adding a line break at the end of everytex.print
- Substituting
tex.sprint(...)
totex.sprint(table.concat({...},''))
, but it also didn't work
Note: By the way, if you know of a better way to loop that can also help, im trying using lua
because it seems possible to me, and the other ways i saw people using loops seemed hard to understand or very verbose and unpractical.
tex.print
....
. This makes it much less likely to get answers.