代码:

代码:

假设我有两个 tex 文件file1.texfile2.tex。我想将它们合并到另一个 tex 文件中。我尝试使用以下文件执行此操作:

\documentclass{book}  
\begin{document}  
\chapter{Chapter-1}  
\section{Section-1}
\begin{theorem}
Theorem 1.1.1
\end{theorem}

\input{file1}
\input{file2}


\begin{lemma}
Lemma   
\end{lemma}
\end{document}  

但这显示错误。可以这样做吗?

file1.tex 如下所示:
\input{mks}

\begin{document}





\begin{center}
{\textbf{\huge{Abstract Algebra}}}
\end{center}


\noindent\large{\textbf{Part-B}}
\begin{enumerate}
 \item Consider a group  $G$. Let $Z(G)$ be its centre. i.e.,$Z(G)=\{g \in G : gh=hg \mbox{~for all~} h \in G\}$. For $n \in \mathbf{N}$, the set of
 positive integers , define $J_n=\{(g_1,\dots,g_n)\in Z(G)\times \dots \times Z(G) : g_1\dots g_n=e\}.$ As a subset of the  direct product group 
$G\times \dots \times G$($n$ times direct product of the group $G$), $J_n$ is 
\begin{enumerate}
 \item not necessarily a subgroup.
\item a subgroup but not necessarily a normal subgroup.
\item a normal subgroup.
\item isomorphic to the direct product $Z(G)\times \dots \times Z(G)$($(n-1)$ times).
\end{enumerate}


 \item Let $G$ be a group of order $77$. Then the center of $G$ is isomorphic to
\begin{enumerate}[(a)]
\begin{multicols}{4}
 \item $\mathbf{Z}_{(1)}$
\item $\mathbf{Z}_{(7)}$
\item $\mathbf{Z}_{(11)}$
\item $\mathbf{Z}_{(77)}$
\end{multicols}
\end{enumerate}

\end{enumerate}




\end{document}  

其中 mks.tex 是:

\documentclass[11pt,twoside,a4paper]{article}
\usepackage{amsthm,amsmath,amssymb,graphicx}
\usepackage{enumerate}

\usepackage[margin=0.45in]{geometry}
\usepackage{multicol}
\usepackage{tikz}
\DeclareMathOperator{\rank }{rank }
\DeclareMathOperator{\trace }{trace }
\DeclareMathOperator{\lcm }{lcm }
\DeclareMathOperator{\nullity }{nullity }  

 and file2.tex is :  


\documentclass{book}
\usepackage[a4paper,margin=.5in]{geometry} 

\usepackage{amsthm,xypic,graphicx}
\theoremstyle{plain}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\theoremstyle{definition}
\newtheorem*{example}{Example}
\newtheorem{definition}{Definition}
\theoremstyle{remark}
\newtheorem*{remark}{Remark}

\begin{document}
\markboth{Right}{Left}
\chapter{First Chapter}
\section{Section 1.1}
\begin{theorem}
 Theorem
\end{theorem}
\begin{lemma}
 lemma1
\end{lemma}

\begin{theorem}
 Theorem
\end{theorem}














$$
\xymatrix{& S\ar@{-}[ld]\ar@{-}[rd] &\\
\{0,a,b,s\}\ar@{-}[d] & &\{0,c\}\ar@{-}[ldd]\\
\{0,a\}\ar@{-}[rd] & & \\
& \{0\} &
}
$$
\begin{center}
 figure A.1 : chosen subsets of $S$ see book b119
\end{center}



$$
\xymatrix{ h_{C}(C) \ar[r]^{\eta(C)}\ar[d]^{h_C(f)} & T(C)\ar[d]^{T(f)} \\
h_C(X)\ar[r]^{\eta(X)} & T(X) 
}
$$
\begin{center}
 figure B.1 : commutativity of the rectangle
\end{center}


$$
\xymatrix{(h_C,T)\ar[rr]^{\theta= \theta_{C,T}}\ar[d]^{N_*(\alpha)} & & T(C)\ar[d]^{\alpha(C)}\\
(h_C,S)\ar[rr] & & S(C)
}
$$
\begin{center}
 figure B.3 : commutativity of the rectangle
\end{center}



$$
\xymatrix{(h_C,T)\ar[rr]^{\theta_C= \theta_{C,T}}\ar[d]^{N_*(f)} & & T(C)\ar[d]^{T(f)}\\
(h_D,T)\ar[rr]^{\theta_D=\theta{D,T}} & & T(D)
}
$$
\begin{center}
 figure B.4 : commutativity of the rectangle
\end{center}



$$
\xymatrix{h_C(C)\ar[r]^{\eta_{(C)}}\ar[d]^{h_C(f)} & & T(C)\ar[d]^{T(f)}\\
h_C(D)\ar[rr]^{\eta_{(D)}} & & T(D)
}
$$
\begin{center}
 figure B.5 : commutativity of the rectangle
\end{center}





$$
\xymatrix{A(U)\ar[rr]^{h(U)}\ar[d]^{r_{_{V,U}}} & & B(U)\ar[d]^{T_{U,V}}\\
A(V)\ar[rr]^{h(V)} & & B(V)
}
$$
\begin{center}
 figure B.6 : commutativity of the rectangle
\end{center}




\end{document}

答案1

我会推荐包裹standalone

在此处输入图片描述

笔记:

代码:

\documentclass{book}

\usepackage{standalone}
\usepackage{xcolor}
\usepackage[paperheight=12.0cm]{geometry}

\usepackage{filecontents}
\begin{filecontents*}{file1.tex}
    \documentclass{book}
    \begin{document}\color{blue}  
        \chapter{Chapter-1}  
        \section{Section-1}
        Text from File 1.
    \end{document}
\end{filecontents*}

\begin{filecontents*}{file2.tex}
    \documentclass{book} 
    \begin{document}  \color{red}
        \chapter{Chapter-1}  
        \section{Section-1}
        Text from file 2.
    \end{document}
\end{filecontents*}


\begin{document}
\input{file1}
\input{file2}

\color{brown}
\chapter{Main File}
Text in main file.
\end{document} 

答案2

作为最佳实践,我按照以下方式安排您的场景,以使其更具通用性。您有:

  • 一个包mypackage.sty,其中放置了主输入文件和子文件(子文件)中使用的所有包。

    % mypackage.sty
    \NeedsTeXFormat{LaTeX2e}[1994/06/01] 
    \ProvidesPackage{mypackage}[2013/05/01 v0.01 LaTeX package for my own purpose]
    \RequirePackage{amsmath}
    % put other packages here
    \endinput
    
  • 第一个输入文件input1.tex作为子文件(子文件)如下。它加载mypackage.sty包。

    % input1.tex
    \documentclass[preview,border=12pt]{standalone}
    \usepackage{mypackage}
    \begin{document}
    Karl's students do not care about arrow tips.
    \[
    E \ne mc^2
    \]
    \end{document}
    
  • 第二个输入文件input2.tex作为子文件如下。它还导入mypackage.sty包。

    % input2.tex
    \documentclass{article}
    \usepackage{mypackage}
    \begin{document}
    Karl's students do not care about dashing patterns.
    \[
    pV = nRT
    \]
    \end{document}
    
  • 主要输入文件main.tex如下。它必须加载mypackage.stydocmute包。docmute包用于导入\begin{document}和内部的所有内容(导入的子文件的内容) \end{document}

    % main.tex
    \documentclass{article}
    \usepackage{mypackage}
    \usepackage{docmute}
    
    \begin{document}
    \input{input1}
    \input{input2}
    \end{document}
    

下面模拟您的场景。使用 进行编译-shell-escape

\documentclass{article}
\usepackage{filecontents}

% creating a package
\begin{filecontents*}{mypackage.sty}
\NeedsTeXFormat{LaTeX2e}[1994/06/01] 
\ProvidesPackage{mypackage}[2013/05/01 v0.01 LaTeX package for my own purpose]
\RequirePackage{amsmath}
% put other packages here
\endinput
\end{filecontents*}

% creating the first input file
\begin{filecontents*}{input1.tex}
\documentclass[preview,border=12pt]{standalone}
\usepackage{mypackage}
\begin{document}
Karl's students do not care about arrow tips.
\[
E \ne mc^2
\]
\end{document}
\end{filecontents*}

% creating the second input file
\begin{filecontents*}{input2.tex}
\documentclass{article}
\usepackage{mypackage}
\begin{document}
Karl's students do not care about dashing patterns.
\[
pV = nRT
\]
\end{document}
\end{filecontents*}

% creating the main input file
\begin{filecontents*}{main.tex}
\documentclass{article}
\usepackage{mypackage}
\usepackage{docmute}

\begin{document}
\input{input1}
\input{input2}
\end{document}
\end{filecontents*}

\usepackage{hyperref}
\begin{document}
\immediate\write18{pdflatex main}
please open \url{main.pdf}
\end{document}

笔记

有些人使用文档类和包的组合standalone。 而另一些人使用subfiles文档类和包的组合。

但我相信我上面的方法比这两种方法灵活得多,因为包含的输入文件可以使用任何文档类。

答案3

combine如果要创建一个包含多个其他 TeX 文档的新文档,请使用 documentclass 。运行texdoc combine以获取以下文档:combine

相关内容