For what it's worth, before I came back here to see @Werner's elegant solution, I took a stab at it. I will incorporate it into his example.
\documentclass{article}
\usepackage{filecontents,lipsum}% http://ctan.org/pkg/{filecontents,lipsum}
% Create some dummy files
\begin{filecontents*}{file1}
\section{File1Section1}\lipsum[1]
\section{File1Section2}\lipsum[2]
\end{filecontents*}
\begin{filecontents*}{file2}
\section{File2Section1}\lipsum[1]
\section{File2Section2}\lipsum[2]
\end{filecontents*}
\newcounter{toplevel}
\setcounter{toplevel}{1}
%
\newcommand{\open}{
\setcounter{section}{0}
\renewcommand*{\thesection}{\arabic{toplevel}.\arabic{section}}
}
%
\newcommand{\close}{
\addtocounter{toplevel}{1}
\setcounter{section}{\value{toplevel}}
\renewcommand*{\thesection}{\arabic{toplevel}}
}
%
\renewcommand*{\thesubsection}{\arabic{toplevel}.\arabic{section}.\arabic{subsection}}
\begin{document}
\section{Section1}
\open
\input{file1}
\input{file2}
\subsection{Just a subsection}
\close
\section{Section2}
\open
\input{file1}
\input{file2}
\close
\end{document}
It sort of works except that the it needs better formatting so the \thesubsection
is in a smaller font and also the \tableofcontents
doesn't recognize the demotion of level so the printing is not very pretty. And apart from aesthetics, any \thesubsection
in the main file will be further demoted (in my kludgey way) when included between the \open
and \close
statements. Anyway it's what I could do with my knowledge of \LaTeX
(basically, \newcounter
, \setcounter
, and \renewcommand
).
\subsection
headings in the input file?\subsection
-level headings in the file and I had not thought of relative sectioning but will have to keep in mind in the future, though @Werner's solution is exactly what I need!