knitr
has a few pretty straightforward ways of handling this.
Option 1: Using knit_child()
with inline R code
Say your setup is like the following. In the same directory, you have:
graph.R
## ---- graph
library(ggplot2)
CarPlot <- ggplot() +
stat_summary(data= mtcars,
aes(x = factor(gear),
y = mpg
),
fun.y = "mean",
geom = "bar"
)
CarPlot
chapter1.Rnw
Hey, look, a graph (Figure~\ref{fig:graph})!
<<graph, echo=FALSE, message=FALSE, fig.lp='fig:', out.width='.5\\linewidth', fig.align='center', fig.cap="A graph", fig.pos='h!'>>=
@
main.Rnw
\documentclass{article}
\begin{document}
<<external-code, echo=FALSE, cache=FALSE>>=
read_chunk('./graph.R')
@
\Sexpr{knit_child('chapter1.Rnw')}
\end{document}
Then, you can knit
the main.Rnw
file and compile the resulting .tex
file with either pdflatex
or xelatex
.
The output is:
![enter image description here](https://i.stack.imgur.com/xF1MT.png)
Note that you can also read the external .R
file from the child .Rnw
file.
So, the following would have worked just as well.
chapter1-mod.Rnw
<<external-code, echo=FALSE, cache=FALSE>>=
read_chunk('./graph.R')
@
Hey, look, a graph (Figure~\ref{fig:graph})!
<<graph, echo=FALSE, message=FALSE, fig.lp='fig:', out.width='.5\\linewidth', fig.align='center', fig.cap="A graph", fig.pos='h!'>>=
@
main-mod.Rnw
\documentclass{article}
\begin{document}
\Sexpr{knit_child('chapter1-mod.Rnw')}
\end{document}
Option 2: Using chunk option child
Assuming you have graph.R
and chapter1.Rnw
from above in the same directory, then your main.Rnw
should be:
\documentclass{article}
\begin{document}
<<external-code, echo=FALSE, cache=FALSE>>=
read_chunk('./graph.R')
@
<<child-demo, child='chapter1.Rnw'>>=
@
\end{document}
Note that you can also read the external .R
file from within the child document in this case, too.
So, assuming you had graph.R
and chapter1-mod.Rnw
from above in the same directory, then your main-mod.Rnw
file should be:
\documentclass{article}
\begin{document}
<<child-demo, child='chapter1-mod.Rnw'>>=
@
\end{document}
When you say
\newtheorem{abc}{Abc}[section]
\newtheorem{xyz}[abc]{Xyz}
you're telling LaTeX that the statement environments abc
and xyz
must share the counter; so no counter xyz
is defined.
You could do
\theoremstyle{definition}
\newtheorem{Gcal}{Grande Caloria \textit{Cal} o \textit{kcal}}[section]
\newtheorem{Pcal}[Gcal]{Piccola Caloria \textit{cal}}
\newtheorem{CalS}[Gcal]{Calore Specifico}
Note that one \theoremstyle{definition}
declaration suffices.
Actually there's a better way.
\documentclass[12pt,a4paper]{report}
\usepackage[T1]{fontenc}
\usepackage[italian]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsthm,mathtools}
\usepackage{amsmath, textcomp}
\usepackage{titling}
\theoremstyle{definition}
\newtheorem{vardef*}{\vardefname}[section]
\newcommand\vardefname{} % initialize
\newenvironment{vardef}[1]
{\renewcommand\vardefname{#1}\begin{vardef*}}
{\end{vardef*}}
\begin{document}
\chapter{1}
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
\chapter{2}
\section{2.1}
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
\begin{vardef}{Grande Caloria \textit{Cal} o \textit{kcal}}
blah blah blah blah blah blah blah blah blah blah blah blah blah blah
\end{vardef}
\begin{vardef}{Piccola Caloria \textit{cal}}
blah blah blah blah blah blah blah blah blah blah blah blah blah blah
\end{vardef}
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
\begin{vardef}{Calore Specifico}
blah blah blah blah blah blah blah blah blah blah blah blah blah blah
\end{vardef}
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
\end{document}
You have just a generic environment for definitions and give the title at statement time rather than having a different environment for each in the preamble.
![enter image description here](https://i.stack.imgur.com/U53b1.png)
Best Answer
You are not providing any screenshot for
Example
style, so I've defined my own, and the codings are :Please paste the above codings after
\begin{document}
and then try...