Variables with Input from Separate File












0















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

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

newcommandStrawberries{34}
newcommandApples{14}
newcommandGrapes{431}
newcommandPears{56}
newcommandTomatoes{42}
newcommandTotalFruit{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?










share|improve this question







New contributor




Cordelia Marie David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • the file that you input should just be a list of newcommand remove the documentclass etc.

    – David Carlisle
    6 hours ago
















0















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

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

newcommandStrawberries{34}
newcommandApples{14}
newcommandGrapes{431}
newcommandPears{56}
newcommandTomatoes{42}
newcommandTotalFruit{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?










share|improve this question







New contributor




Cordelia Marie David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • the file that you input should just be a list of newcommand remove the documentclass etc.

    – David Carlisle
    6 hours ago














0












0








0








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

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

newcommandStrawberries{34}
newcommandApples{14}
newcommandGrapes{431}
newcommandPears{56}
newcommandTomatoes{42}
newcommandTotalFruit{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?










share|improve this question







New contributor




Cordelia Marie David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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

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

newcommandStrawberries{34}
newcommandApples{14}
newcommandGrapes{431}
newcommandPears{56}
newcommandTomatoes{42}
newcommandTotalFruit{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?







input






share|improve this question







New contributor




Cordelia Marie David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Cordelia Marie David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Cordelia Marie David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 7 hours ago









Cordelia Marie DavidCordelia Marie David

1




1




New contributor




Cordelia Marie David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Cordelia Marie David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Cordelia Marie David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • the file that you input should just be a list of newcommand remove the documentclass etc.

    – David Carlisle
    6 hours ago



















  • the file that you input should just be a list of newcommand remove the documentclass etc.

    – David Carlisle
    6 hours ago

















the file that you input should just be a list of newcommand remove the documentclass etc.

– David Carlisle
6 hours ago





the file that you input should just be a list of newcommand remove the documentclass etc.

– David Carlisle
6 hours ago










3 Answers
3






active

oldest

votes


















1














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 :



newcommandStrawberries{34}
newcommandApples{14}
newcommandGrapes{431}
newcommandPears{56}
newcommandTomatoes{42}
newcommandTotalFruit{thenumexpr Strawberries + Apples + Grapes + Pears + Tomatoes relax}





share|improve this answer
























  • 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?

    – Cordelia Marie David
    6 hours ago













  • @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 newcommandStrawberries{{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.

    – David Carlisle
    6 hours ago



















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}





share|improve this answer

































    0














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



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


    ... with some other values, all in the document (table, plot and text) will change accordingly:



    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. To compile it, 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).






    share|improve this answer
























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "85"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });






      Cordelia Marie David is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f483846%2fvariables-with-input-from-separate-file%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      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 :



      newcommandStrawberries{34}
      newcommandApples{14}
      newcommandGrapes{431}
      newcommandPears{56}
      newcommandTomatoes{42}
      newcommandTotalFruit{thenumexpr Strawberries + Apples + Grapes + Pears + Tomatoes relax}





      share|improve this answer
























      • 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?

        – Cordelia Marie David
        6 hours ago













      • @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 newcommandStrawberries{{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.

        – David Carlisle
        6 hours ago
















      1














      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 :



      newcommandStrawberries{34}
      newcommandApples{14}
      newcommandGrapes{431}
      newcommandPears{56}
      newcommandTomatoes{42}
      newcommandTotalFruit{thenumexpr Strawberries + Apples + Grapes + Pears + Tomatoes relax}





      share|improve this answer
























      • 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?

        – Cordelia Marie David
        6 hours ago













      • @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 newcommandStrawberries{{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.

        – David Carlisle
        6 hours ago














      1












      1








      1







      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 :



      newcommandStrawberries{34}
      newcommandApples{14}
      newcommandGrapes{431}
      newcommandPears{56}
      newcommandTomatoes{42}
      newcommandTotalFruit{thenumexpr Strawberries + Apples + Grapes + Pears + Tomatoes relax}





      share|improve this answer













      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 :



      newcommandStrawberries{34}
      newcommandApples{14}
      newcommandGrapes{431}
      newcommandPears{56}
      newcommandTomatoes{42}
      newcommandTotalFruit{thenumexpr Strawberries + Apples + Grapes + Pears + Tomatoes relax}






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 6 hours ago









      David CarlisleDavid Carlisle

      498k4111441893




      498k4111441893













      • 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?

        – Cordelia Marie David
        6 hours ago













      • @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 newcommandStrawberries{{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.

        – David Carlisle
        6 hours ago



















      • 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?

        – Cordelia Marie David
        6 hours ago













      • @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 newcommandStrawberries{{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.

        – David Carlisle
        6 hours ago

















      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?

      – Cordelia Marie David
      6 hours ago







      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?

      – Cordelia Marie David
      6 hours ago















      @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 newcommandStrawberries{{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.

      – David Carlisle
      6 hours ago





      @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 newcommandStrawberries{{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.

      – David Carlisle
      6 hours ago











      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}





      share|improve this answer






























        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}





        share|improve this answer




























          1












          1








          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}





          share|improve this answer















          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}






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 3 hours ago

























          answered 4 hours ago









          Jeffrey J WeimerJeffrey J Weimer

          501119




          501119























              0














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



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


              ... with some other values, all in the document (table, plot and text) will change accordingly:



              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. To compile it, 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).






              share|improve this answer




























                0














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



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


                ... with some other values, all in the document (table, plot and text) will change accordingly:



                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. To compile it, 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).






                share|improve this answer


























                  0












                  0








                  0







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



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


                  ... with some other values, all in the document (table, plot and text) will change accordingly:



                  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. To compile it, 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).






                  share|improve this answer













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



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


                  ... with some other values, all in the document (table, plot and text) will change accordingly:



                  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. To compile it, 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).







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 4 hours ago









                  FranFran

                  53.6k6120183




                  53.6k6120183






















                      Cordelia Marie David is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      Cordelia Marie David is a new contributor. Be nice, and check out our Code of Conduct.













                      Cordelia Marie David is a new contributor. Be nice, and check out our Code of Conduct.












                      Cordelia Marie David is a new contributor. Be nice, and check out our Code of Conduct.
















                      Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f483846%2fvariables-with-input-from-separate-file%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                      Calculate evaluation metrics using cross_val_predict sklearn

                      Insert data from modal to MySQL (multiple modal on website)