2

I am using Overleaf with compiler pdfLaTeX (not sure if this is important), and want to have subsections of different colors, such as the example shown below after the end of the question.

It all works and is fine for compiling. However, as my subsections could get quite large, I would like to be able to fold the \topic{}{} functions just like you are able to do with the \section{} and \subsection{} functions (see final image below).

Is there any way to tell the editor that I would like this function to do this?

Note that I don't particularly care whether defining the new function \topic{}{} is the best way to achieve what I want (that can be a separate question if needed). Rather I am interested here in whether it is possible in general to code-fold with some arbitrarily defined function - my \topic{}{} function is just a short little example.

\documentclass{article}

\usepackage{blindtext}

\setlength{\parindent}{0em}
\setlength{\parskip}{0em}

\usepackage{xcolor}
\newcommand{\topic}[2]{\color{#1}\subsection*{#2}}

\begin{document}

\section{Hello}

\topic{blue}{This subsection is in blue}
\blindtext

\topic{orange}{This subsection is in orange}
\blindtext

\topic{purple}{This subsection is in purple}
\blindtext

\end{document}

enter image description here

enter image description here

2

So I've been playing around a little and found the \newenvironment{} command which essentially allows you to put your function in a \begin{} \end{} sandwich, and Overleaf lets you fold these just like I wanted (see https://www.overleaf.com/learn/latex/Environments#Defining_environments_with_parameters)

I suppose a (minor) disadvantage of this would be that you need to make sure every \begin{} has an \end{}, though the compiler will be quick to shout at you if this is not the case!

I would still be interested to know if you could do this without having to use \begin{} though.

\documentclass{article}

\usepackage{blindtext}

\setlength{\parindent}{0em}
\setlength{\parskip}{0em}

\usepackage{xcolor}
\newenvironment{topic}[2]{\color{#1}\subsection*{#2}}{}

\begin{document}

\section{Hello}

\begin{topic}{blue}{This subsection is in blue}
\blindtext
\end{topic}

\begin{topic}{orange}{This subsection is in orange}
\blindtext
\end{topic}

\begin{topic}{purple}{This subsection is in purple}
\blindtext
\end{topic}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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