Note: The ideas in this question and accepted answer have been composed into a CTAN package called "modular".
Consider the following subimportlevel macro:
packages.tex
\usepackage{import}
\usepackage{coseoul}
\newcommand{\subimportlevel}[3]{
\setcounter{currentlevel}{#3}
\subimport*{#1}{#2}
\setcounter{currentlevel}{#3}
}
The purpose of the macro is to import a modular piece of a document while getting the section/subsection levels right, regardless of what imported the sub-document.
The problem with this macro is that the subimported file can change currentlevel so that the final \setcounter{currentlevel}{#3} doesn't do the right thing (it's supposed to reset currentlevel to whatever it was before the \subimport.
Here's an example usage illustrating the problem:
main.tex
\documentclass{article}
\input{packages.tex}
\author{Daniel Sank}
\title{Example}
\begin{document}
\maketitle
\subimportlevel{./}{content}{5}
\end{document}
content.tex
\subimportlevel{./}{section1}{\value{currentlevel}} % <-- can change currentlevel inside section1.tex
\subimportlevel{./}{section2}{\value{currentlevel}}
section1.tex
\levelstay{Section 1}
This is the first section
\leveldown{A subsection}
This is a subsection
\levelstay{Another subsection}
This is another subsection of the first section.
section2.tex
\levelstay{Section 2}
This is the second section
\leveldown{Subsection}
This should be a subsection of the second section.
\levelstay{Another subsection}
This is supposed to be another subsection of the second section.
Building main.tex results in the following output

Note that the second section is numbered as a subsection.
How can I fix this?
Is there a way to expand the \value{currentlevel} before the importing happens?
\levelstayand\leveldown? Where are those macros defined? – May 17 '15 at 04:44\sectionpreclude rational programming. – DanielSank May 17 '15 at 18:41