# How can I make xcolor work from standalone documents?

I'm trying to use basic colors of the package xcolor from standalone documents, but the following error is returned:

main/filename.tex|29␣error|␣Package␣xcolor␣Error:␣Undefined␣color␣MAGENTA'.


The main document has this:

\documentclass[10pt, b6paper, twoside, openright]{memoir}
\setlrmarginsandblock{1.25cm}{.5cm}{*}
\setulmarginsandblock{1.5cm}{*}{1}
\checkandfixthelayout{}
\usepackage{float}
\usepackage{titlesec}
\usepackage[dvipsnames]{xcolor}
\usepackage{minitoc}
\usepackage{array}
\usepackage{pdfpages}
\usepackage{standalone}
\begin{document}
\tableofcontents{}
\dominitoc{}
\input{mwedoc}
\end{document}



My standalone document (mwedoc) has the following:

\documentclass{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{titlesec}
\usepackage{float}
\usepackage{minitoc}
\usepackage{array}
\begin{document}
\chapter{Chapter Title}
\begin{table}[htbp]
\begin{center}
\begin{tabular}[c]{ m{(\textwidth-0.5in)/2} m{(\textwidth-0.5in)/2} }
\Large{\textcolor{magenta}First} &
\Large{\textcolor{magenta}Second} \\
\textcolor{red}{1. Mondays} &
\textcolor{red}{1. Mondays} \\
\textcolor{red}{2. Saturdays} &
\textcolor{red}{2. Thursdays} \\ &
\textcolor{red}{3. Sundays:}
\end{tabular}
\end{center}
\end{table}
\thispagestyle{fancy}
\minitoc{}
\newpage{}
\end{document}

• I'm pretty sure that \textcolor{red}{text that follows} is the same as {\color{red}text that follows} and \textcolor{magenta}Subtitle is the same as {\color{magenta}S}ubtitle. I don't know the answer to your main question. Aug 12 '21 at 13:21
• Unrelated \Large does not take any arguments, but \textcolor takes two so \textcolor{magenta} Subtitle would make the S magenta. You should load xcolor in both documents, we cannot run the MWE you present here as xcolor is not loaded, and I would assume that \chapter and \minitoc or the fancy pagestyle is available in that MWE. Aug 12 '21 at 13:35
• I should probably start over with this question. None of the code you provided makes any sense of have any chance to be able to compile. Aug 12 '21 at 13:37
• @Teepeemm Mostly, but not entirely. \textcolor issues an explicit \leavevmode first. There are situations where using \color alone leads to puzzling output. Aug 12 '21 at 14:17
• Thanks for all the help, guys. I finally made it to work by working on a better MWE. I will edit the question to make it clearer. Thanks again, all! Aug 12 '21 at 15:03

standalone package's basic default behaviour is that, for sub-files, it "ignores anything till the next \begin{document} and then takes the document environment as a simple group" (texdoc standalone, s6.1 (p22)) - that is, in default mode, it skips over the sub-file's preamble, including the \documentclass{} command.

To demonstrate, take two input files: they both have the same content (a tabular), but one file is standalone class and colours the page background via a preamble command; and the other file is article class and colours the table itself with a \colorbox{}{} command.

The first sub-file is called input1.tex and when compiled on its own produces this:

MWE

\documentclass[border=6pt]{standalone}
\usepackage{xcolor}
\pagecolor{blue!12}
\begin{document}
\fbox{\begin{tabular}[l]{ ll }
\Large\textcolor{magenta}First1 &
\Large\textcolor{magenta}Second2 \\
\textcolor{red}{1. Mondays} &
\textcolor{red}{1. Mondays} \\
\textcolor{red}{2. Saturdays} &
\textcolor{red}{2. Thursdays} \\ &
\textcolor{red}{3. Sundays:}
\end{tabular}
}
\end{document}


The second file, input2.tex produces this as the first item on a standard-size default page with a page number (page size and page numbering are defined by the document class):

The full page:

MWE

\documentclass{article}
\usepackage{xcolor}
\pagecolor{red!2}
\usepackage{array}
\begin{document}
\fbox{\colorbox{green}{\begin{tabular}[l]{ ll }
\Large\textcolor{magenta}First2 &
\Large\textcolor{magenta}Second2 \\
\textcolor{red}{1. Mondays} &
\textcolor{red}{1. Mondays} \\
\textcolor{red}{2. Saturdays} &
\textcolor{red}{2. Thursdays} \\ &
\textcolor{red}{3. Sundays:}
\end{tabular}
}}
\end{document}


Now, using standalone package in the main file and inputting both subfiles produces this:

In both cases, the sub-file preamble was skipped over, and just whatever was between \begin{document} and \end{document was imported into the main document.

The first sub-file's \pagecolor{} command, being in the preamble, has no effect on the input; the second sub-file's \colorbox{}{} command, being in the document body, carried over into the input.

In other words, standalone package's default is as if whatever is between \begin{document} and \end{document is just copy-pasted into the main document.

Sidenote: standalone package has options relating to how to deal with preamble code, e.g., consolidating multiple package options. Plus, in some cases, in particular where loading order of packages is important, sub-file preambles might need to be "carefully merged into the main preamble" and standalone package has an option to assist with this task (p23).

Now, standalone class (original intention: produce a single image in a cropped PDF canvas) does not have pages as such, so floats have nowhere to float to (so figure is useless), there are no headers to set because there are no margins (and therefore also there is no centering), chapters and other sectioning commands have no context and so error out (therefore no TOC). Etc. That reduces the actual input file to just the tabular (including corrected column-sizing code by @JohnKormylo from the other answer):

Note: magenta is one of the basic 19 xcolor colours, so option [dvipsnames] is not needed. Magenta (with capital M) is a dvispsnames colour name (texdoc xcolor lists all the (defined) colour names).

Note 2: using \documentclass[class=memoir]{standalone} allows some usable settings from the underlying class to come through (font sizing and spacing settings). The default underlying class is article.

MWE

\documentclass[class=memoir]{standalone}
\usepackage{xcolor}
%\usepackage{fancyhdr}
%\usepackage{titlesec}
%\usepackage{float}
%\usepackage{minitoc}
\usepackage{array}
\begin{document}
%    \chapter{Chapter Title}
%    \begin{table}[htbp]
%        \begin{center}
\begin{tabular}[c]{ m{\dimexpr 0.5\textwidth-0.25in} m{\dimexpr 0.5\textwidth-0.25in} }
\Large{\textcolor{magenta}First} &
\Large{\textcolor{magenta}Second} \\
\textcolor{red}{1. Mondays} &
\textcolor{red}{1. Mondays} \\
\textcolor{red}{2. Saturdays} &
\textcolor{red}{2. Thursdays} \\ &
\textcolor{red}{3. Sundays:}
\end{tabular}
%        \end{center}
%    \end{table}
%    \thispagestyle{fancy}
%    \minitoc{}
%    \newpage{}
\end{document}


inputting that produces:

MWE

\documentclass[10pt, b6paper, twoside, openright]{memoir}
\setlrmarginsandblock{1.25cm}{.5cm}{*}
\setulmarginsandblock{1.5cm}{*}{1}
\checkandfixthelayout{}
\usepackage{float}
%\usepackage{titlesec}
\usepackage{xcolor}
\usepackage{fancyhdr}
\usepackage{minitoc}
\usepackage{array}
\usepackage{pdfpages}
\usepackage{standalone}
\begin{document}
%    \tableofcontents{}
%    \dominitoc{}
\input{input1}
\input{input2}
\par\bigskip
\input{input3}
\end{document}


For colours, use either the switch {\color{magenta} text} or the command \textcolor{magenta}{text} to colour "text" as magenta.

Although, having said all that, the usage of standalone is more nuanced: standalone class has options like crop=false, multi=... and even float=..., although, again, "In general it is recommended to keep floating environments inside the main document and only place the content of them into standalone files. This also makes it simple to include the same content in different floats of different main documents. If custom floats are defined using a package like float [they] are not supported yet. Dependent on the way they define floats they might still work." (p12)

In this case, it could probably be more efficient just to use memoir class instead of standalone class in the input files.

• This was great! @Cicada, thanks for your time and for all the clarification! Aug 13 '21 at 12:11

Not an answer, but a working memoir MWE.

There is no point in including things like \chapter or \minitoc in standalone class. One can include standalone as a package (see section 6 of manual), but as far as I can tell, all it really does is define \includestandalone.

\documentclass{memoir}
\usepackage[dvipsnames]{xcolor}
\usepackage{titlesec}
\usepackage{float}
\usepackage{minitoc}
\usepackage{array}
\usepackage{showframe}% MWE only
\begin{document}
\chapter{Chapter Title}
\begin{table}[htbp]
\centering
\fbox{\begin{tabular}[c]{ m{\dimexpr 0.5\textwidth-0.25in} m{\dimexpr 0.5\textwidth-0.25in} }
\textcolor{magenta}{\Large First} &
\textcolor{magenta}{\Large Second} \\
\textcolor{red}{1. Mondays} &
\textcolor{red}{1. Mondays} \\
\textcolor{red}{2. Saturdays} &
\textcolor{red}{2. Thursdays} \\ &
\textcolor{red}{3. Sundays:}
\end{tabular}}
\end{table}
\thispagestyle{fancy}
\minitoc{}
\newpage{}
\end{document}


The OP seems to be confusing two concepts:

1. importing code vs.

2. importing a graphic.

If you wish to import code, you use \input in the main document; however, the imported document behaves as if you inserted it (via typing) into the main document. Thus, the imported document cannot have a \documentclass, \usepackages, or \begin and \end{document}. It inherits those characteristics from the main document.

On the other hand, if you wish to import a graphic, the standalone file should be fully self-sufficient with its own \documentclass, \usepackages, and \begin and \end{document}s. In that case though, you first compile the standalone document creating a PDF. Then, in the main document, you don't use \input, but \includegraphics{mwedoc} to display the PDF graphic of mwedoc.pdf. Whether you place the \includegraphics inside a figure` environment or not is a question of style, but the basic concept is the same.

• Oh my..., now I understand the difference. Thank you! @Steven Aug 13 '21 at 12:13