3

I would like to have text in two colored boxes next to each other. I found \tcbox, but it automatically adds a linebreak.

Please note: I can use anything that works with pdflatex. No need for \tcbox, it was only the closest thing to what I'm looking for.

Minimal Example

\documentclass[a4paper]{scrartcl}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{sectsty}
\usepackage{tcolorbox}

\definecolor{darkblue}{HTML}{00008A}
\definecolor{lightblue}{HTML}{C8C8FA}
\sectionfont{\color{darkblue}}
\subsectionfont{\color{darkblue}}
\tcbset{colback=darkblue,arc=0mm,auto outer arc,boxrule=0mm,width=1.5cm, height=1cm,center title,box align=base}

\pagestyle{fancy}
\fancyhf{}
\rhead{\textcolor{darkblue}{2017}}
\lhead{\tcbox{\textcolor{white}1 \textcolor{white}\textbar}\tcbox[colback=lightblue]{Chapter Name}}
\renewcommand{\familydefault}{\sfdefault}

\begin{document}
\section{And so it begins}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum
dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero
eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no
sea takimata sanctus est Lorem ipsum dolor sit amet.
\end{document}

Rendered Minimal Example

enter image description here

  • I just found colorbox - looks good, except I would like a bit more space – Martin Thoma Sep 11 '17 at 8:39
3

You're missing the on line option, but also to set a strut in the two boxes, so the text baselines will be aligned too.

I'd use \newtcbox rather than a global \tcbset. Don't forget to set the \headheight (fancyhdr suggests 28.45274pt, I rounded to 30pt).

Beware that KOMA classes claim that using fancyhdr with them is not recommended; you should consult the guide in order to use scrlayer-scrpage for the job.

\documentclass[a4paper]{scrartcl}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{sectsty}
\usepackage{tcolorbox}

\definecolor{darkblue}{HTML}{00008A}
\definecolor{lightblue}{HTML}{C8C8FA}
\sectionfont{\color{darkblue}}
\subsectionfont{\color{darkblue}}

\newtcbox{\headtcbox}[1][]{%
  colback=darkblue,
  arc=0mm,
  auto outer arc,
  boxrule=0mm,
  width=1.5cm,
  height=1cm,
  center title,
  box align=base,
  on line,
  #1,
}

\pagestyle{fancy}
\fancyhf{}
\rhead{\textcolor{darkblue}{2017}}
\lhead{%
  \headtcbox{\strut\textcolor{white}{1 \textbar}}%
  \headtcbox[colback=lightblue]{\strut Chapter Name}%
}
\setlength{\headheight}{30pt}

\renewcommand{\familydefault}{\sfdefault}

\begin{document}

\section{And so it begins}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum
dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero
eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no
sea takimata sanctus est Lorem ipsum dolor sit amet.

\end{document}

enter image description here

4

A simple tabular with coloured columns will do:

\documentclass[a4paper]{article}%{scrartcl}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage{array}
\usepackage{graphicx}
\usepackage{sectsty}
\usepackage[table]{xcolor}
\definecolor{darkblue}{HTML}{00008A}
\definecolor{lightblue}{HTML}{C8C8FA}
\sectionfont{\color{darkblue}}
\subsectionfont{\color{darkblue}}
\setlength\headheight{14pt}
\pagestyle{fancy}
\fancyhf{}
\rhead{\textcolor{darkblue}{2017}}
\lhead{\setlength\extrarowheight{2pt}\begin{tabular}{@{}>{\columncolor{darkblue}[0em][\tabcolsep]\color{white}\bfseries\enspace }c>{\cellcolor{lightblue}}l}
1 \textbar & Chapter Name
\end{tabular}}
\renewcommand{\familydefault}{\sfdefault}

\begin{document}

\section{And so it begins}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum
dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero
eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no
sea takimata sanctus est Lorem ipsum dolor sit amet.

\end{document} 

enter image description here

3

A MWE worth a thousand words:

mwe

\documentclass{article}
\usepackage{xcolor} 
\begin{document}
\fcolorbox{orange}{yellow}{\strut foo}
\fcolorbox{orange}{yellow}{\strut foo}

\fcolorbox{orange}{yellow}{foo}
\fcolorbox{orange}{yellow}{oops}
\setlength\fboxsep{1em}
\fcolorbox{orange}{yellow}{oops}
\setlength\fboxrule{3pt}
\fcolorbox{orange}{yellow}{oops}
\end{document}

If you do not want the space between boxes, joint the boxes in the same line or add % just after the boxes. Otherwise LaTeX with take the line break as an space.

Of course, you can add vertical padding via \strut also with \colorbox, but not whole internal with \fboxsep, or external padding with \fboxrule using a white rule.

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.