Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have several reports I've written up in article format.

I want to keep them in separate files but combine them into a larger report. I've removed the headers, but don't want to go through each file and relabel each section as a subsection, and so on. Is there a way to handle this programatically? For instance, in my main file I might have

\section{Section1}
\input{file1}

and in file1.tex I have

\section{File1Section1}
...
\section{File1Section2}

I think some solution might be the redefinition of \section before and after each \input invocation, or an environment in which I can wrap the \input invocation where each \section is renamed as \subsection and so on. Help?

share|improve this question
    
Are there \subsection headings in the input file? –  egreg Feb 15 '13 at 22:32
    
Related: Demoting/Promoting sections, chapters, etc –  Werner Feb 15 '13 at 22:52
1  
Not the perfect solution for existing documents but when you next time start a new project and know this might become an issue, consider using relative sectioning: stackoverflow.com/questions/2066119/… –  Christian Feb 15 '13 at 23:03
    
There are indeed \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! –  crippledlambda Feb 15 '13 at 23:09
add comment

2 Answers 2

up vote 8 down vote accepted

The following redefinition of \input redefines \section to be \subsection (as well as for the subsidiary sectioning commands) via \demotesections. Of course, this assumes that there are no deeper levels inside the included files than \subparagraph. However, it would be possible to set these to \relax as a "no-op":

enter image description here

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

\let\oldinput\input
\newcommand{\savesections}{%
  \let\oldsection\section% Store \section
  \let\oldsubsection\subsection% Store \subsection
  \let\oldsubsubsection\subsubsection% Store \subsubsection
  \let\oldparagraph\paragraph% Store \paragraph
  \let\oldsubparagraph\subparagraph% Store \subparagraph
}\AtBeginDocument{\savesections}
\newcommand{\restoresections}{%
  \let\section\oldsection% Restore \section
  \let\subsection\oldsubsection% Restore \subsection
  \let\subsubsection\oldsubsubsection% Restore \subsubsection
  \let\paragraph\oldparagraph% Restore \paragraph
  \let\subparagraph\oldsubparagraph% Restore \subparagraph
}
\newcommand{\demotesections}{%
  \let\section\subsection% Modify \section to be \subsection
  \let\subsection\subsubsection% Modify \subsection to be \subsubsection
  \let\subsubsection\paragraph% Modify \subsubsection to be \paragraph
  \let\paragraph\subparagraph% Modify \paragraph to be \subparagraph
  %\let\subparagraph\relax% Make \subparagraph a no-op
}
\renewcommand{\input}[1]{%
  \demotesections% Demote sections
  \oldinput{#1}% Input file
  \restoresections}% Restore sections
\begin{document}
\section{Section1}
\input{file1}
\input{file2}
\subsection{Just a subsection}
\section{Section2}
\input{file1}
\input{file2}
\end{document}

The sectioning units are first saved using \savesections. Redefinition via \demotesections is "localized" within the new macro \input as a result of a subsequence \restoresections, allowing to use the default sectional units outside without loss of functionality.

share|improve this answer
    
THIS IS PERFECT THANK YOU! –  crippledlambda Feb 15 '13 at 23:09
    
This is mostly, academic but with the other page you linked to, I can wrap each \input invocation with a \levelup or \leveldown environment to have full control over the section depth of the input files I've already created. This is really marvelous. –  crippledlambda Feb 15 '13 at 23:28
add comment

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).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.