1

I just started learning Latex for work but I'm having trouble with a code I'm writing. I'm trying to create a file that will take variables from another file and print them out with their assigned values in different settings. I have another file that has all the variables stored and will later input variables from matlab there.

This file is the one with my variables:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{calc}
\input xintexpr.sty\relax

\title{Example of Variable Input}
\author{Cordelia David}
\date{April 2019}

\newcommand\Strawberries{34}
\newcommand\Apples{14}
\newcommand\Grapes{431}
\newcommand\Pears{56}
\newcommand\Tomatoes{42}
\newcommand\TotalFruit{\xintexpr \Strawberries + \Apples + \Grapes + \Pears + \Tomatoes \relax}

\begin{document}

\maketitle

Strawberries = \Strawberries\

Apples = \Apples\

Grapes = \Grapes\

Pears = \Pears\

Tomatoes = \Tomatoes\

Total of all Fruit = \TotalFruit\

\end{document}

Which prints out mostly correctly, though I'm unsure why there's an exclamaton mark in front of the number for Total of All Fruits when compiled.

This is the file that calls that file:

\documentclass{article} \usepackage[utf8]{inputenc}

\title{Example of Variable Input} \author{Cordelia David} \date{April 2019} \input{Variables.tex} \begin{document}



\maketitle

\section{Introduction}

We had \Strawberries\ Strawberries for this year's harvest. Probably not enough.

\end{document}

The problem I'm having is it will only compile the variables file, and anything I write in the main text won't get printed. Why is this? How do I fix this?

1
  • the file that you input should just be a list of \newcommand remove the \documentclass etc. Apr 8, 2019 at 18:50

3 Answers 3

6
\documentclass{article} \usepackage[utf8]{inputenc}

\title{Example of Variable Input}
\author{Cordelia David} \date{April 2019}
\input{variables.tex}

\begin{document}



\maketitle

\section{Introduction}

We had \Strawberries\ Strawberries for this year's harvest. Probably not enough.

\end{document}

with variables.tex :

\newcommand\Strawberries{34}
\newcommand\Apples{14}
\newcommand\Grapes{431}
\newcommand\Pears{56}
\newcommand\Tomatoes{42}
\newcommand\TotalFruit{\the\numexpr \Strawberries + \Apples + \Grapes + \Pears + \Tomatoes \relax}
2
  • Thank you that worked perfectly! I didn't know you could have a latex document without the document class, but it seems silly now that I didn't realize this. Would there be any way to create a substructure for one variable? Like adding different years to the harvest of one fruit? Apr 8, 2019 at 18:58
  • @CordeliaMarieDavid you should perhaps ask a new question but tex has no structure at all just macros so you can make whatever structure you like eg \newcommand\Strawberries{{2017}{30}{2018}{34}{2019}{2}} then instead of using \Strawberries directly define some macro to extract the right information. You could store a list like this, or store it one macro per fruit per year or .... the details of the "extraction" depend of course how you choose to store it but anything is possible. Apr 8, 2019 at 19:18
1

Analysis of the Problem

Reading between the lines of what you posted, I see an external database being used to fill a document.

Proposed Approach

I will cut to the chase and recommend the datatool package.

Preamble

  • article class

  • load the two packages

  • generate the database (can be removed when the database already exists)

  • load the database

Document

  • count the total fruit

  • do various things to show the counts of various fruit

Code

\documentclass{article}

% datatool does the database work
% xifthen parses for alternative input choices

\usepackage{datatool,xifthen}

% the command to get how many fruit
% use \howmany{fruitname} for the count with designation.
% use \howmany[0]{fruitname} for the count without its designation.

\newcommand*{\howmany}[2][1]{%
    \ifthenelse{\equal{#1}{0}}
        {\DTLfetch{fruits}{fruit}{#2}{count}}%
        {\DTLfetch{fruits}{fruit}{#2}{count} #2}%
    }

% the file contents
% this is generated for the first time here
% it could instead be a CSV file that you have generated elsewhere
% (i.e. using a spreadsheet program)
% you will only need to generate this database ONE TIME

\begin{filecontents*}{fruitdatabase.csv}
fruit, count
strawberries, 34
bananas, 10
cherries, 11
apples, 4
\end{filecontents*}

% this command loads the databasebase to the document

\DTLloaddb{fruits}{fruitdatabase.csv}

\begin{document}

% this command sums the count column and stores the result
% in the variable \totalcount

\DTLsumcolumn{fruits}{count}{\totalcount}

% here are some example use cases

We had \howmany{strawberries} today.

We had \howmany[0]{apples} golden yellow apples yesterday.

We had \totalcount{} total fruits in the last two days.

\end{document}
0

Even if almost anything could be made via LaTeX macros (if you are David Carlisle), to use variables, and make something complex with that variables, a pure LaTeX solution is not the easier way. Consider the example below, taking the help of the R power. If you change the line ...

Amounts <- c(34,14,431,56,42)

... or the fruits name, or you add some other fruit, all in the document (table, plot and text) will change accordingly. Of course, the constructed data frame df could be also a comma delimited CSV file loaded with the read.csv function (an example here).

Fruits.pdf file:

MWE

Fruits.Rnw file:

\documentclass{article}
\usepackage{booktabs}
\begin{document}

<<echo=F,results='asis'>>=
library(xtable) 
Fruits <- c("Strawberries","Apples","Grapes","Pears","Tomatoes")
Amounts <- c(34,14,431,56,42)
df <- data.frame(Fruits,Amounts)
print(xtable(df, digits=0), booktabs=T)
@

There are \Sexpr{combine_words(tolower(df$Fruits))}. 
In total there are \Sexpr{sum(df$Amounts)} fruits. 
We have a lot of \Sexpr{tolower(df$Fruits[df$Amounts==max(df$Amounts)])} 
(\Sexpr{max(df$Amounts)}),
but we need \Sexpr{toupper(df$Fruits[df$Amounts==min(df$Amounts)])} !!

<<echo=F,results='asis',fig.height=4 >>=
barplot(df$Amounts,legend=df$Fruits,col=rainbow(5))
@

\end{document}

To compile this you must have R and knitr package installed in your system, not only the TeX distribution. The easiest way is load this file in Rstudio and click on "compile PDF" (this will convert Fruits.Rnw in Fruits.tex and then in Fruits.pdf).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .