1

This is a follow up to my question Change color system for part of a document?

I tried to solve this problem by defining the document to be blue (or whatever color I want) then to switch the colors back to normal for my particular environment, and then to switch them back. This is clearer from an example. However, my issue is not with the colorswitching it works perfectly.

My problem is that I can not use any blank lines in my new command!

For instance

\iSol{ \lipsum[75] } 

works perfectly but,

\iSol{ \lipsum[75]

\lipsum[66] }

Complains about a missing par. Any idea how I can fix the command so I can use blank lines in it?

Code

\documentclass{article}

% Uses the default color profile from UiT's official guidelines

\usepackage{xcolor,lipsum}
\usepackage{xparse,etoolbox}
\newtoggle{isLF} 

\definecolor{UiT-main}{HTML}{003349}
\definecolor{UiT-red}{HTML}{CB333B}
\definecolor{UiT-blue}{HTML}{007396}
\definecolor{UiT-cyan}{HTML}{59BEC9}
\definecolor{UiT-orange}{HTML}{F2A900}
% The green is not default, but very useful
\colorlet{UiT-green}{green!50!black}
\colorlet{UiT-solution}{black}

\colorlet{UNI-default}{.}
\colorlet{UNI-current}{UNI-default}

\colorlet{UNI-monochrome}{black!50!white}

\newcommand{\resetUiTcolors}{
  \definecolor{UiT-main}{HTML}{003349}
  \definecolor{UiT-red}{HTML}{CB333B}
  \definecolor{UiT-blue}{HTML}{007396}
  \definecolor{UiT-cyan}{HTML}{59BEC9}
  \definecolor{UiT-orange}{HTML}{F2A900}
  % The green is not default, but very useful
  \colorlet{UiT-green}{green!50!black}
}

\newcommand{\monoUiTcolors}{
  \colorlet{UNI-monochrome}{black!50!white}

  \colorlet{UiT-solution}{black}

  \colorlet{UiT-main}{UNI-monochrome}
  \colorlet{UiT-red}{UNI-monochrome}
  \colorlet{UiT-blue}{UNI-monochrome}
  \colorlet{UiT-cyan}{UNI-monochrome}
  \colorlet{UiT-orange}{UNI-monochrome}
  % The green is not default, but very useful
  \colorlet{UiT-green}{UNI-monochrome}
}

\newcommand{\UNIfullcolor}[1]{
  \colorlet{UNI-default}{.}
  \resetUiTcolors
  \resetUNIhypersetup
  \color{UiT-solution}
  #1
  \monoUiTcolors
  \monoUNIhypersetup
  \color{UNI-default}
}

\DeclareDocumentCommand{\iSol}{m G{}}{
  \iftoggle{isLF}{
      \UNIfullcolor{#1}
  }{#2}
}

% Hyperlenker og klikkbare lenker.
\RequirePackage{hyperref}

\hypersetup{
  pdftoolbar=true,        % show Acrobat’s toolbar?
  pdfmenubar=false,       % show Acrobat’s menu?
  pdffitwindow=false,     % window fit to page when opened
  pdfstartview={FitH},    % fits the width of the page to the window
  linktoc=all,            % Link all the things in the toc
  colorlinks=true,        % false: boxed links; true: colored links
  linkcolor=black,        % color of internal links (change box color with linkbordercolor)
  citecolor=UiT-cite,        % color of links to bibliography
  filecolor=UiT-orange,      % color of file links
  urlcolor=UiT-url          % color of external links
}

\newcommand{\resetUNIhypersetup}{%
  \hypersetup{
    colorlinks=true,        % false: boxed links; true: colored links
    linkcolor=black,        % color of internal links (change box color with linkbordercolor)
    citecolor=UiT-cite,        % color of links to bibliography
    filecolor=UiT-orange,      % color of file links
    urlcolor=UiT-url          % color of external links
  }%
}
\newcommand{\monoUNIhypersetup}{%
  \hypersetup{
    colorlinks=true,        % false: boxed links; true: colored links
    linkcolor=UNI-monochrome,        % color of internal links (change box color with linkbordercolor)
    citecolor=UNI-monochrome,        % color of links to bibliography
    filecolor=UNI-monochrome,      % color of file links
    urlcolor=UNI-monochrome          % color of external links
  }%
}

\begin{document}

\color{blue}

\lipsum[5]

\settoggle{isLF}{true}

% this works
\iSol{\lipsum[75]}

% this dont

%\iSol{\lipsum[75]
%
%\lipsum[66]}

\lipsum[66]

\end{document}
2
  • Please report error messages correctly. It doesn't complain about a missing \par, it says ! Paragraph ended before \iSol was complete. That's not quite the same...
    – campa
    Dec 7 '19 at 11:01
  • Prefer \NewDocumentCommand
    – egreg
    Dec 7 '19 at 11:10
2

While in LaTeX2e \newcommand by default creates \long macros (while the starred version creates short ones), \DeclareDocumentCommand makes the opposite. In order to allow paragraphs in a macro argument you must prefix the specifier by a +, in your case:

\DeclareDocumentCommand{\iSol}{+m G{}}{% <-- don't forget this
  \iftoggle{isLF}{% <-- don't forget this
      \UNIfullcolor{#1}% <-- don't forget this
  }{#2}% <-- don't forget this
}

Beware that \DeclareDocumentCommand is the xparse version of \def, so it won't check whether the macro exists. \NewDocumentCommand is the safer version.

6
  • 1
    Beware of spurious spaces and also recommend \NewDocumentCommand
    – egreg
    Dec 7 '19 at 11:11
  • @egreg Ups, I wrote that so quickly that I hadn't noticed that... Thx, I'll update
    – campa
    Dec 7 '19 at 11:12
  • Wow, thanks for a great answer. I will be sure to switch to \NewDocumentCommand. However, even with adding % I still ran into the following problem. Now it inserts a superflous newline after the command i.imgur.com/0qnpqWx.png However inserting an empty line fixes it i.imgur.com/WAoWCNp.png What gives? Dec 7 '19 at 11:20
  • @N3buchadnezzar Sorry, I can't reproduce that; everything works fine for me.
    – campa
    Dec 7 '19 at 11:29
  • Strange, when I pasted in my code again with your changes everything worked. I will wait a few hours and accept your answer, to give your answer a bit more exposure. Been struggling with this problem for a few weeks and can not believe the solution was that simple! Dec 7 '19 at 11:32

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.