I must say I've been using LaTeX for only a couple of months, so you could say I'm still a beginner.
I have a large project that has many modifications applied to it. Every single time I wanted to change something about my document's look and feel, I searched it up on the internet and copy-pasted it in my document. So, you can say it has gone pretty large, and I'm curious if it's a bad thing, and if there is any room for optimization (ex: I did something using a package, but I could've done it using another package I already had included). Basically, I'd like to see what someone with more LaTeX experience than me has to say about what I've thrown together, and any suggestions, improvements, way to organize better etc.
Here's the modifications I've done:
\documentclass[
12pt, % Font size
a4paper % Paper type
]{book}
% Packages
\usepackage[
margin=2.7cm, % Margin size
marginparwidth=2cm, % Margin note size
marginparsep=3mm, % Space between margin and text
headheight=15pt % Header height (fix that stupid warn from fancyhdr)
]{geometry}
\usepackage[romanian]{babel} % Romanian characters support
\usepackage{indentfirst} % Add paragraph indentation even after a section
\usepackage{marginnote} % Notes on the margins of a document (more advanced \marginpar)
\usepackage{titlesec} % Customize titles
\usepackage{hyperref} % Hyperlink support
\usepackage{graphicx} % Image support
\usepackage{xcolor} % Custom colors
\usepackage{fancyhdr} % Custom headers
\usepackage[titles]{tocloft} % Customize ToC
% Image path
\graphicspath{ {../img/} }
% Hyperlink configuration
\hypersetup{
colorlinks=true,
urlcolor=black,
linkcolor=black % ToC links color
}
% Colors (for chapter)
\definecolor{gray75}{gray}{0.75}
% Custom format for titles, sections etc.
\titleformat{\part}[display]
{\Huge\scshape\filright}
{\partname~\thepart:}
{20pt}
{\thispagestyle{empty}}
\titleformat{\chapter}[hang]
{\Large}
{\thechapter \ifnum\value{chapter} > 9 {\hspace{7pt}}\else{\hspace{15pt}}\fi \textcolor{gray75}{|}\hspace{15pt}}
{0pt}
{\thispagestyle{chapterfancystyle}\Large}
\renewcommand{\thesection}{\arabic{section}}
\titleformat*{\section}
{\large\bfseries}
\titleformat{\subsection}
{\normalfont\normalfont\bfseries}
{}
{1.5em}
{}
% Custom commands
% Format: \newcommand{\command}[variable]{action #variable}
\newcommand{\rom}[1]{\uppercase\expandafter{\romannumeral #1\relax}} % Roman numerals
\newcommand{\textbfit}[1]{\textbf{\textit{#1}}} % combine bold and italic
\newcommand{\operatitle}{} % to not get errors
\newcommand{\operaauthor}{} % to not get errors
% Customize \marginnote font
\renewcommand\marginfont{\ttfamily\footnotesize}
% Make \ttfamily hyphenate words for the margin notes
\DeclareFontFamily{OT1}{cmtt}{\hyphenchar\font=-1}
\DeclareFontFamily{\encodingdefault}{\ttdefault}{\hyphenchar\font=`\-}
\DeclareFontFamily{T1}{cmtt}{\hyphenchar\font=45}
% ToC customization
\setcounter{tocdepth}{0} % Make only chapters appear in ToC
\renewcommand{\cftdot}{} % Remove ToC dots
% Disable bold in ToC
\addtocontents{toc}{\string\renewcommand{\protect\cftchappagefont}{\protect\normalfont}}
\addtocontents{toc}{\string\renewcommand{\protect\cftchapfont}{\protect\normalfont}}
\addtocontents{toc}{\string\renewcommand{\protect\cftchapleader}{\protect\normalfont\protect\cftdotfill{\protect\cftsecdotsep}}}
% Custom fancy styles
\fancypagestyle{chapterfancystyle}{
\fancyhf{}
\fancyfoot[LE,RO]{\thepage}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{1pt}
}
\fancypagestyle{plain}{
\fancyhf{}
\fancyhead[LE]{\small\textsc{Esee pentru bacalaureat}}
\fancyhead[RO]{
\begingroup
\small
\let\textbfit\relax % smart thing to use \textsc in header but keep it bfit everywhere else
\textsc{\operatitle\ -- \operaauthor}
\endgroup
}
\fancyfoot[LE,RO]{\thepage}
\renewcommand{\headrulewidth}{2pt}
\renewcommand{\footrulewidth}{1pt}
}
% Use empty pagestyle on blank pages between chapters
\makeatletter
\def\cleardoublepage{\clearpage\if@twoside \ifodd\c@page\else
\hbox{}\thispagestyle{empty}\newpage\if@twocolumn\hbox{}\newpage\fi\fi\fi}
\makeatother
\begin{document}
\input{titlepage.tex} % Import the title page
% Use empty pagestyle for the entirety of the ToC
\addtocontents{toc}{\protect\thispagestyle{empty}}
\clearpage
{
\pagestyle{empty}
\fancypagestyle{plain} % create temporary plain pagestyle
{
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
}
\tableofcontents % actually generate the ToC
\thispagestyle{empty}
}
\pagestyle{plain} % revert to plain pagestyle
[...]
\end{document}
Also, here's the entire TeX document, if there is more context needed
EDIT: Thanks to everyone's suggestions, I made small changes, and also made everything into my own custom documentclass. This brings the actual document down to only:
\documentclass{eseuri_bac_romana}
\begin{document}
\maketitle % Insert the custom title page
\CustomToC % Insert the custom ToC
[...]
\end{document}
And here is the .cls
file:
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{eseuri_bac_romana}[2022/03/25 My custom class for the essay book]
\LoadClass[
12pt, % Font size
a4paper % Paper type
]{book}
% Packages
\RequirePackage[
margin=2.7cm, % Margin size
marginparwidth=2cm, % Margin note size
marginparsep=3mm, % Space between margin and text
headheight=15pt % Header height (fix that stupid warn from fancyhdr)
]{geometry}
\RequirePackage[romanian]{babel} % Romanian characters support
\RequirePackage{indentfirst} % Add paragraph indentation even after a section
\RequirePackage{marginnote} % Notes on the margins of a document (more advanced \marginpar)
\RequirePackage{titlesec} % Customize titles
\RequirePackage{graphicx} % Image support
\RequirePackage{xcolor} % Custom colors
\RequirePackage{fancyhdr} % Custom headers
\RequirePackage[titles]{tocloft} % Customize ToC
\RequirePackage{hyperref} % Hyperlink support, keep last in package list!
% Image path
\graphicspath{ {../img/} }
% Hyperlink configuration
\hypersetup{
colorlinks=true,
urlcolor=black,
linkcolor=black % ToC links color
}
% Colors (for chapter)
\definecolor{gray75}{gray}{0.75}
% Custom format for titles, sections etc.
\titleformat{\part}[display]
{\Huge\scshape\filright}
{\partname~\thepart:}
{20pt}
{\thispagestyle{empty}}
\newcommand{\DetermineHSpaceSize}{\ifnum\value{chapter} > 9 {\hspace{7pt}}\else{\hspace{15pt}}\fi} % Make the hspace that goes between chapter number and line smaller if number is past 10
\titleformat{\chapter}[hang]
{\Large}
{\thechapter\DetermineHSpaceSize\textcolor{gray75}{|}\hspace{15pt}}
{0pt}
{\thispagestyle{chapterfancystyle}\Large}
\renewcommand{\thesection}{\arabic{section}} % Remove chapter number from section
\titleformat*{\section}
{\large\bfseries}
\titleformat{\subsection}
{\normalfont\normalfont\bfseries}
{}
{1.5em}
{}
% Custom commands
% Format: \newcommand{\command}[variable]{action #variable}
\newcommand{\rom}[1]{\uppercase\expandafter{\romannumeral #1\relax}} % Roman numerals
\newcommand{\textbfit}[1]{\textbf{\textit{#1}}} % combine bold and italic
\newcommand{\operatitle}{} % to not get errors
\newcommand{\operaauthor}{} % to not get errors
\newcommand{\CustomToC} % command to generate ToC with empty pagestyle
{
\addtocontents{toc}{\protect\thispagestyle{empty}}
\clearpage % new page to ensure it switched to empty pagestyle
{
\pagestyle{empty}
\fancypagestyle{plain} % create temporary plain pagestyle
{
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
}
\tableofcontents % actually generate the ToC
\thispagestyle{empty}
}
\pagestyle{plain} % revert to plain pagestyle
}
% Customize \marginnote font
\renewcommand\marginfont{\ttfamily\footnotesize}
% Make \ttfamily hyphenate words for the margin notes
\DeclareFontFamily{OT1}{cmtt}{\hyphenchar\font=-1}
\DeclareFontFamily{\encodingdefault}{\ttdefault}{\hyphenchar\font=`\-}
\DeclareFontFamily{T1}{cmtt}{\hyphenchar\font=45}
% ToC customization
\setcounter{tocdepth}{0} % Make only chapters appear in ToC
\renewcommand{\cftdot}{} % Remove ToC dots
\addtocontents{toc}{\string\renewcommand{\protect\cftchappagefont}{\protect\normalfont}}
\addtocontents{toc}{\string\renewcommand{\protect\cftchapfont}{\protect\normalfont}}
\addtocontents{toc}{\string\renewcommand{\protect\cftchapleader}{\protect\normalfont\protect\cftdotfill{\protect\cftsecdotsep}}} % Disable bold in ToC
% Custom fancy styles
\fancypagestyle{chapterfancystyle}{
\fancyhf{}
\fancyfoot[LE,RO]{\thepage}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{1pt}
} % style for chapter page
\fancypagestyle{plain}{
\fancyhf{}
\fancyhead[LE]{\small\textsc{Eseuri pentru bacalaureat}}
\fancyhead[RO]{
\begingroup
\small
\let\textbfit\relax % smart thing to use \textsc in header but keep it bfit everywhere else
\textsc{\operatitle\ -- \operaauthor}
\endgroup
}
\fancyfoot[LE,RO]{\thepage}
\renewcommand{\headrulewidth}{2pt}
\renewcommand{\footrulewidth}{1pt}
} % style for the rest of the document
% Use empty pagestyle on blank pages between chapters
\makeatletter
\def\cleardoublepage{\clearpage\if@twoside \ifodd\c@page\else
\hbox{}\thispagestyle{empty}\newpage\if@twocolumn\hbox{}\newpage\fi\fi\fi}
\makeatother
% Create custom title
\renewcommand{\maketitle}
{
\begin{titlepage}
[...]
\end{titlepage}
}
\begin{document}
. If you need to design a complicated title page you can put that in commands in the preamble. Otherwise this is a pretty typical preamble for a document with customizations..sty
) file so I can keep my focus on the writing in the main document. You just start with\NeedsTeXFormat{LaTeX2e} \ProvidesPackage{mystyle}[2022/03/25 custom style]
, then put everything you have here but write\RequirePackage
instead of\includepackage
, and end with\endinput
. Then in the main document you just have\includepackage{mystyle}
..sty
file as @musarithmia suggested, you can also just copy all the lines from your preamble, paste them into aheader.tex
file, and issue\input{header.tex}
. But honestly, the amount of stuff you have is pretty tame. It is only long because you opted for legibility (otherwise a lot of newlines can be suppressed). If you find scrolling past the preamble annoying, try to see if you can set up some sort of folding in your text editor..sty
file can be worth the time.hyperref
further down your package list. It should typically be loaded last (with the exception of very few packages which need to be loaded after it).