问题分析

问题分析

我刚开始学习 Latex 以备工作之用,但我编写的代码遇到了问题。我试图创建一个文件,该文件将从另一个文件中获取变量,并在不同的设置下打印出它们及其指定的值。我有另一个文件,其中存储了所有变量,稍后会从 matlab 输入变量。

这个文件是包含我的变量的文件:

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

打印结果基本正确,但我不确定为什么编译时所有水果总数前面有一个感叹号。

这是调用该文件的文件:

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

我遇到的问题是它只会编译变量文件,而我在正文中写的任何内容都不会被打印。这是为什么?我该如何解决这个问题?

答案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}

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

问题分析

从您所发布的内容中,我发现有一个外部数据库正在用于填充文档。

建议的方法

我将直奔主题并推荐 datatool 包。

前言

  • 文章类别

  • 加载两个包

  • 生成数据库(当数据库已存在时可以删除)

  • 加载数据库

文档

  • 计算水果总数

  • 做各种事情来显示各种水果的数量

代码

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

答案3

即使几乎任何东西都可以通过 LaTeX 宏(如果您是 David Carlisle)制作,使用变量,并使用这些变量制作复杂的东西,纯 LaTeX 解决方案并不是更简单的方法。考虑下面的例子,借助 R 的力量。如果你改变行...

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

...或水果名称,或者您添加其他水果,文档(表格、图表和文本)中的所有内容都会相应更改。当然,构建的数据框df也可以是加载了该函数的逗号分隔的 CSV 文件read.csv(示例这里)。

Fruits.pdf文件:

平均能量损失

Fruits.Rnw文件:

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

要编译此文件,您必须knitr在系统中安装 R 和软件包,而不仅仅是 TeX 发行版。最简单的方法是将此文件加载到 Rstudio 中,然后单击“编译 PDF”(这将转换Fruits.RnwFruits.tex,然后转换为 Fruits.pdf)。

相关内容