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:

\input xintexpr.sty\relax

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

\newcommand\TotalFruit{\xintexpr \Strawberries + \Apples + \Grapes + \Pears + \Tomatoes \relax}



Strawberries = \Strawberries\

Apples = \Apples\

Grapes = \Grapes\

Pears = \Pears\

Tomatoes = \Tomatoes\

Total of all Fruit = \TotalFruit\


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}



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


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?

  • 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

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

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




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


with variables.tex :

\newcommand\TotalFruit{\the\numexpr \Strawberries + \Apples + \Grapes + \Pears + \Tomatoes \relax}
  • 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

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.


  • article class

  • load the two packages

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

  • load the database


  • count the total fruit

  • do various things to show the counts of various fruit



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


% 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.

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

fruit, count
strawberries, 34
bananas, 10
cherries, 11
apples, 4

% this command loads the databasebase to the document



% this command sums the count column and stores the result
% in the variable \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.


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:


Fruits.Rnw file:


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)])} 
but we need \Sexpr{toupper(df$Fruits[df$Amounts==min(df$Amounts)])} !!

<<echo=F,results='asis',fig.height=4 >>=


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 .