What's the best way of typing the following 58 equations into LaTeX?
Here's a possible implementation; the delimiters can be changed.
\documentclass{article}
\usepackage{amsmath,multicol}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\constraints}{omO{x}}
{
\IfValueTF{#1}{\begin{multicols}{#1}\centering}{\begin{center}}
\egreg_costraints:nn { #2 } { #3 }
\IfValueTF{#1}{\end{multicols}}{\end{center}}
}
\seq_new:N \l__egreg_constraints_seq
\cs_new_protected:Nn \egreg_costraints:nn
{
\seq_set_split:Nnn \l__egreg_constraints_seq { \\ } { #1 }
\seq_map_inline:Nn \l__egreg_constraints_seq
{
\__egreg_constraints_item:nn { ##1 } { #2 }
}
}
\cs_new_protected:Nn \__egreg_constraints_item:nn
{
\tl_if_blank:nF { #1 }
{
$#2\sb{\clist_item:nn { #1 } { 1 }} + #2\sb{\clist_item:nn { #1 } { 2 }}\ge1$ \\
}
}
\ExplSyntaxOff
\begin{document}
\constraints{
1,2 \\ 3,4 \\ 5,6
}
\constraints{
1,2 \\ 3,4 \\ 5,6
}[y]
\constraints[3]{
1,2 \\ 3,4 \\ 5,6 \\
1,2 \\ 3,4 \\ 5,6 \\
1,2 \\ 3,4 \\ 5,6 \\
1,2 \\ 3,4 \\ 5,6 \\
1,2 \\ 3,4 \\ 5,6 \\
1,2 \\ 3,4 \\ 5,6 \\
1,2 \\ 3,4 \\ 5,6 \\
1,2 \\ 3,4 \\ 5,6 \\
1,2 \\ 3,4 \\ 5,6 \\
}
\end{document}
You can automate the job by exporting the Excel spreadsheet to a comma delimited file (for example subscripts.csv
) and reading your data with the readarray
package.
MWE
Assuming that the content of subscripts.csv
is:
1,2
1,3
2,39
7,5
26,5
The following code:
\documentclass{article}
\usepackage{readarray}
\usepackage{forloop}
\newcommand\constraint[2]{$x_{#1} + x_{#2} \ge 1$}
\begin{document}
\readarraysepchar{,}
\readdef{subscripts.csv}{\MyMacro}
\readarray\MyMacro\MyData[-,\ncols]
\newcounter{MyCounter}
\forloop{MyCounter}{1}{\value{MyCounter} < \MyDataROWS}
{
\constraint{\MyData[\theMyCounter,1]}{\MyData[\theMyCounter,2]}\par
}
\end{document}
renders:
Use Excel's CONCAT
function.
It's probably easiest to do this in Excel, especially if it's a one-time thing. Excel has several text-manipulation functions, and I have often found it a very convenient way to generate several similar lines of text or code. CONCAT(text1,text2,...)
concatenates a bunch of text and data values into one text output.
If columns A and B contain your i and j values, enter the following in cell C1:
=CONCAT("x_{", A1, "} + x{", B1, "} \ge 1.")
and hit Enter. You can then copy-paste the function all the way down column C and get the result you want. You can then select all the output cells in column C and paste into your text editor.