# Environment undefined ONLY when referencing a preamble.tex

I'm trying to use R Markdown to typeset some notes and I'm having difficulty using the \adjustwidth environment. The issue only arises when I reference some preamble.tex. For instance, if I have a blank preamble.tex, and I run the following, I get an error.

---
- \usepackage{changepage}
output:
pdf_document:
includes:
---
Test


However, if I don't reference preamble.tex (which is just blank) it works!

---
- \usepackage{changepage}
output: pdf_document
---
Test


And I don't think it's an issue with preamble.tex because a) it's literally blank b) I can reference it as long as I don't also use \adjustwidth. E.g, the following works.

---
- \usepackage{changepage}
output:
pdf_document:
includes:
---
Test


Any idea?

Resulting mwe.tex:

\documentclass[]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\begin{document}
Test
\end{document}

• Welcome to TeX.SE! I see only r code in your question. Can you please show the resulting compilable tex code? At the moment your questions (pure r question) is off-topic for me ... – Mensch Apr 13 at 15:19
• Thanks! I'm very new to latex and I'm not sure how to put this into tex code. I also don't know if the same issue would arise. Is there a better place for me to ask this question? I don't see an R SE. Thanks! – Danny Apr 13 at 15:50
• Hmm... what does it produce on your system, when it works? A PDF directly? It probably produces all kinds of intermediate files – among them a .tex file – before producing the PDF. There should be a line of code you can include in your rmd (or whichever you use for that) the will prevent the output TeX file to be deleted. I can't look it up right now, but please try to find it in the documentation. – thymaro Apr 13 at 16:13
• @thymaro thanks for your help. If I'm calling the Rmd file "scratch" it produces scratch.knit.md, scratch.log, scratch.tex, and scratch.utf8.md, as well as the pdf. Does that give you any indication of what I could change? Thanks so much! – Danny Apr 13 at 16:27
• @Danny if you have scratch.tex, this is probably the file being used to produce scratch.pdf by your tex distro, in the end. Would you mind boiling that file down as much as possible, so it still produces the observed error, then posting its contents here? – thymaro Apr 13 at 18:01

I simply do not know r but I can help you with the tex file. Now you can change your r code and compare the result with the mwe I show below.

In your resulting file, let us it name mwe.tex you have to add the call of package changepage to get environment adjustwidth defined. How to do that with r I can not help you, ask for example on page stack overflow and use tag r.

After adding the call to that package environment adjustwidth is defined, but there are two parameters missing for the right and left margin indent.

So you have to change your

\begin{adjustwidth}


to

\begin{adjustwidth}{1cm}{1cm}


So at last you need to get the following compiling code as a result of your r code:

\documentclass[]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
%\usepackage{fixltx2e} % provides \textsubscript

\usepackage{changepage} % <=============================================
\usepackage{showframe}

\begin{document}
Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test
\end{document}


Please see that I commented the call of fixltx2e, because it is not longer needed (added to LaTeX kernel after 2015).

Please see that I only added package showframe to this mwe to visualize the typing area and the margins and the right and lrft indent for adjustwidth.

Then you should get the result:

For anyone interested, I found a way to make it work. For some reason

header_includes:
- \usepackage{changepage}


wasn't working, but the following did

output:
- pdf_document:
- extra_dependencies: ['changepage']


did!

• how did you find that solution? I mean, how did you get the idea to make this change in that way? – thymaro Apr 16 at 18:05