I am trying to semi-automatically import multiple parameters/variables from an excel (via .txt) file into both equation environments and $$ = math environments in (share)latex.
It is semi-automattically because I still need to manually import a .txt file into (share)latex.
My approach consists of the following e.g. test.txt
file:
2: The old conversion coefficient k_{roe,Sv}
9.329664
4: The new conversion coefficient {K_{roe,Sv}}_{new}
0
6: r_{worst}
817278.6
8: r_{best}
81727.9
And the numbers are imported with the following/similair latex lines:
\item $K_{sh} = \lstinputlisting[firstline=28,lastline=28]{data_input/test.txt}$
Where the datapoint/number is selected by varying the line number (e.g. 4 to 6) in both firstline=4
and lastline=4
.
It works fine in the normal (share)latex environment. However, if I apply the command in a $$ environment, I get the following error:
missing $ inserted
and if I add an extra $ sign after the command e.g.
\item $K_{sh} = \lstinputlisting[firstline=28,lastline=28]{data_input/test.txt}$$
it still get that same error.
So I tried putting it in a math Environment with:
\item $K_{sh} = \ensuremath{\lstinputlisting[firstline=28,lastline=28]{data_input/test.txt}}$
and
\item $K_{sh} = \ensuremath{\lstinputlisting[firstline=28,lastline=28]{data_input/test.txt}}$$
As was suggested in this question. However, the missing $ error still remains.
I fear it might be a double question, but with the searchterms known to me, I have not been able to find a working solution. Would you know the solution?
As to comply with BambOo's tip/comment, I have created the following MWE in which the K_{sh..
lines are tested individually by commenting the other 3 K_{sh..
lines out.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\title{MWE import data}
\author{A.T.}
\date{April 2018}
\begin{document}
\maketitle
\section{Introduction}
\ensuremath{test} %Testing whether the \ensuremath command requires a package; it does not.
% $K_{sh} = \lstinputlisting[firstline=2,lastline=2]{test.txt}$
% $K_{sh} = \lstinputlisting[firstline=2,lastline=2]{test.txt}$$
% $K_{sh} = \ensuremath{\lstinputlisting[firstline=2,lastline=2]{test.txt}}$
$K_{sh} = \ensuremath{\lstinputlisting[firstline=2,lastline=2]{test.txt}}$$
\end{document}
It makes use of the following test.txt
file in the same folder as main.tex
:
2: The old conversion coefficient k_{roe,Sv}
9.329664
4: The new conversion coefficient {K_{roe,Sv}}_{new}
0
6: r_{worst}
817278.6
8: r_{best}
81727.9
I did not find out whether it is convention to post the MWE here as code, or whether to link to an external file, I guess w.r.t. availability reduction when the external source goes off line, code is preferred even though it takes more time to recreate.
\documentclass{...}
and ending with\end{document}
.