TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to create a colored box in LaTeX that has two lines. I would like the text to be centered. Ideally, I want to be able to change the text font and the size of the colored box.

So, an example of what I am aiming for is on the left side of the image:

colored box

I have tried numerous things, mostly from a previous post (Set size of \colorbox for a single character). But I am running into errors for different reasons. Here is a MWE.tex that I created with my two attempts that seem the closest to what I am trying to achieve:

\documentclass[paper=letterpaper,fontsize=10pt]{article}
\usepackage{amsmath,amsfonts,amsthm}
\usepackage{graphicx}
\usepackage[svgnames]{xcolor}
\usepackage{geometry}
\usepackage{url}
\usepackage{wrapfig}
\frenchspacing
\pagestyle{empty}
\usepackage{sectsty}
\sectionfont{
    \usefont{OT1}{phv}{b}{n}%
    \sectionrule{0pt}{0pt}{-5pt}{3pt}
    }
\usepackage{eqparbox} 

%%% BEGIN DOCUMENT ------------------------------------------------------------
\begin{document}

\vspace*{-7em}

%First attempt%
\noindent \colorbox{Black}{\parbox{4em} \hfill \color{White} \Huge \usefont{OT1}{phv}{b}{n} FirstName}
\vspace*{-.3mm} %doesn't work
\newline
\noindent \colorbox{Black}{\parbox{4em} \hfill \color{White} \Huge \usefont{OT1}{phv}{b}{n} LastName}

%Second attempt%
\noindent \colorbox{Black}{\makebox(60,30){\textcolor{white}{FirstName}}}
\newline
\vspace*{-2em} %doesn't work
\noindent \colorbox{Black}{\makebox(60,30){\textcolor{white}{LastName}}}
\vspace*{5em}

\vspace{2em}
My CV would follow here

\end{document}

Upon running lualatex MWE.tex, I get an output that shows the results of my two attempts that looks as follows:

MWE output

In both attempts, I am having difficulty getting rid of the obvious distinction between the two boxes separating FirstName and LastName. I began to wonder if there may be an easier solution to my problem at hand, and possibly one in which I do not need to create two separate boxes.

I did try various alternatives, such as:

\noindent \colorbox{Black}{\makebox(60,60){\textcolor{white}{FirstName \n LastName}}}

But keep being unable to reach my desired outcome. Any advice is, as always, much appreciated!

share|improve this question
    
You have forgotten the \interlineskip value. This can be switched of with \offlinterlineskip, but do this in a group only] – Christian Hupfer Feb 8 at 22:51
    
\parbox{4em} \hfill the content of the \parbox is _just` \hfill jut need to put the content of the box inside {} – David Carlisle Feb 8 at 23:17
3  
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – samcarter May 24 at 0:42

This is in fact (much, much, much) easier with tcolorbox

\documentclass[paper=letterpaper,fontsize=10pt]{article}
\usepackage{amsmath,amsfonts,amsthm}
\usepackage{graphicx}
\usepackage[svgnames]{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{geometry}
\usepackage{url}
\usepackage{wrapfig}
\frenchspacing
\pagestyle{empty}
\usepackage{sectsty}
\sectionfont{
    \usefont{OT1}{phv}{b}{n}%
    \sectionrule{0pt}{0pt}{-5pt}{3pt}
  }
%\usepackage{eqparbox} % ??? What's this?

%%% BEGIN DOCUMENT ------------------------------------------------------------
\begin{document}
\begin{tcbraster}[raster columns=2,raster equal height]
\begin{tcolorbox}[enhanced jigsaw, % needed to really the frame off!
 colback=black, % black background
 coltext=white, % white text
 halign=center, % center
 fontupper={\Huge \bfseries}, % change the font here
 sharp corners, % no rounded corners
 colframe=black, % not really necessary
 boxrule=0pt % frame off 
 ]
  FirstName

  LastName
\end{tcolorbox}
\begin{tcolorbox}[enhanced jigsaw,sharp corners,coltext=black,colback=white,boxrule=0pt]
\begin{itemize}
\item Github
\item LinkedIn
\item Phone
\end{itemize}
\end{tcolorbox}
\end{tcbraster}

\end{document}

enter image description here

share|improve this answer

This is another tcolorbox solution which uses the skins and xparse libraries. xparse is used to define a new command

\namebox{<first names>}{<last name>}{<content of right hand box e.g. list>}

The width of the left hand box is adapted to fit the content of the top and bottom parts. The overall structure consists of a sidebyside box which adapts to the width of the left hand box. The left hand side of the box consists of a further tcolorbox with upper and lower parts. The right hand side of the box consists of whatever is in the third argument - in this case, a list.

This is probably easier to demonstrate than it is to explain:

\namebox{First Names}{Last Name}{%
  \begin{itemize}
    \item[\faGithubSquare] github.com/Repository
    \item[\faLinkedinSquare] linkedin.com/Account
    \item[\faMobile] 0123 456789
  \end{itemize}%
}

In this case, the first argument takes more width than the second, so the left hand side will be set to accommodate the width of First Names.

\namebox{Short Name}{Somewhat Longer Name}{%
  \begin{itemize}
    \item[\faGithubSquare] github.com/Repository
    \item[\faLinkedinSquare] linkedin.com/Account
    \item[\faMobile] 0123 456789
  \end{itemize}%
}

In this case, the second argument needs more space than the first, so the left hand box will be sized to accommodate Somewhat Longer Name.

In both cases, the remainder of the line width will be available for the content of the right hand side i.e. in this case the list with icons.

\DeclareRobustCommand{\nameboxfont}{\normalfont\sffamily\bfseries\Large\baselineskip14pt}

sets the font which will be used for the content of the boxes. Adapt as desired.

Two styles are defined which determine the styles used for the boxes.

\tcbset{%
  name box/.style={%

This style is used for the two-storey box on the left.

    colback=black,

Black background.

    lower separated=false,

No line separating the upper bit from the lower.

    coltext=white,

White text.

    fontupper=\nameboxfont,
    fontlower=\nameboxfont,

Applies the font defined earlier to both parts of the box.

    halign=center,
    halign lower=center,

Aligns the text in the upper and lower stories centred.

    sharpish corners,

The default is to use rounded corners. I've used sharpish corners here. Another possibility is sharp corners. Try to see which you like.

    middle=.75mm,

Although the manual suggests otherwise, this seems to be half the distance between the upper and lower parts of the box. (The manual suggests this is in addition to boxsep, but that seems not to be the case.)

  },

That's that style done.

  two by two/.style={%

This is the style for the big box which includes everything.

    sidebyside gap=1mm,

The distance between the left hand black box and the right hand side containing the list.

    lower separated=false,

As above: no line.

    halign upper=left,
    halign lower=left,

Sets the alignment of the left black box left and the alignment of the right hand list to left.

    sidebyside adapt=left,

Adapts the width of the parts to the width of the stuff on the left.

    empty,

No background, lines etc.

    size=minimal,

Don't add extra spacing.

This box is purely structural: its purpose is to align things and not to look like a box in anyway, so we want this stripped-down 'bare' version provided by the library skins.

  },

We're done with this style, too.

}

That's all the styles we need.

We also want a couple of scratch dimensions for when we measure things.

\newlength\nameboxtempa
\newlength\nameboxtempb

Here's the definition of the final command, \namebox{}{}{}.

\NewDocumentCommand \namebox { m m +m }{%
  \settowidth\nameboxtempa{\nameboxfont #1}%
  \settowidth\nameboxtempb{\nameboxfont #2}%

We measure the widths of the first and second arguments.

  \ifdim\nameboxtempa<\nameboxtempb\setlength\nameboxtempa{\nameboxtempb}\fi%

We now compare the widths. If the width of the stuff in the bottom (last name) is greater than the width of the stuff in the top (first names), then we set the width \nameboxtempa to the width of the bottom bit. Otherwise, \nameboxtempa is already the width we want.

  \tcbsidebyside[two by two]{%

Apply our style for the big box.

    \begin{tcolorbox}[%

Start the box on the left.

      name box,

Apply our style for this.

      text width=\nameboxtempa,

Set the width to \nameboxtempa to ensure it just accommodates the longest of the two lines it will contain.

      ]
      #1

First names go here.

      \tcblower
      #2

Last name goes in the bottom part.

    \end{tcolorbox}%

Close the environment and we're done with the left side of the big, outer box.

  }{%
    #3

The right side of that box gets whatever is in the third argument.

  }
}

<code>\namebox</code> demo

Complete code:

\documentclass{article}
\usepackage{tcolorbox,fontawesome}
\tcbuselibrary{xparse,skins}
\DeclareRobustCommand{\nameboxfont}{\normalfont\sffamily\bfseries\Large\baselineskip14pt}
\newlength\nameboxtempa
\newlength\nameboxtempb
\NewDocumentCommand \namebox { m m +m }{%
  \settowidth\nameboxtempa{\nameboxfont #1}%
  \settowidth\nameboxtempb{\nameboxfont #2}%
  \ifdim\nameboxtempa<\nameboxtempb\setlength\nameboxtempa{\nameboxtempb}\fi%
  \tcbsidebyside[two by two]{%
    \begin{tcolorbox}[%
      name box,
      text width=\nameboxtempa,
      ]
      #1
      \tcblower
      #2
    \end{tcolorbox}%
  }{%
    #3
  }
}
\tcbset{%
  name box/.style={%
    colback=black,
    lower separated=false,
    coltext=white,
    fontupper=\nameboxfont,
    fontlower=\nameboxfont,
    halign=center,
    halign lower=center,
    sharpish corners,
    middle=.75mm,
  },
  two by two/.style={%
    sidebyside gap=1mm,
    boxrule=0pt,
    boxsep=0pt,
    lower separated=false,
    halign upper=left,
    halign lower=left,
    sidebyside adapt=left,
    empty,
    size=minimal,
  },
}
\begin{document}
\namebox{First Names}{Last Name}{%
  \begin{itemize}
    \item[\faGithubSquare] github.com/Repository
    \item[\faLinkedinSquare] linkedin.com/Account
    \item[\faMobile] 0123 456789
  \end{itemize}%
}
\namebox{Short Name}{Somewhat Longer Name}{%
  \begin{itemize}
    \item[\faGithubSquare] github.com/Repository
    \item[\faLinkedinSquare] linkedin.com/Account
    \item[\faMobile] 0123 456789
  \end{itemize}%
}
\end{document}
share|improve this answer

enter image description here

\documentclass{article}

\usepackage{color}

\begin{document}

\colorbox{black}{\textcolor{white}{\Large\bfseries\sffamily
\begin{tabular}{@{}l@{}}
FirstName\\
lastName
\end{tabular}}}

\end{document}
share|improve this answer
    
I left aligned, if you want it centred, change l to c – David Carlisle Feb 8 at 23:30
    
But with this how would you place the next box (of a different colour as asked by the OP) adjacent to the first one? – WYSIWYG Feb 9 at 15:13
    
@WYSIWYG I'm not sure I understand your question, boxes in latex are positioned like letters, if you place X next to Y they come side by side unless you leave a blank line and put them in separate paragraphs, – David Carlisle Feb 9 at 16:12
    
I meant that the boxes should be of same size and have some kind of horizontal alignment between them. If I am not mistaken that is what OP is asking. – WYSIWYG Feb 9 at 16:17
    
@WYSIWYG no, the actual question title and the MWE just asks how to do the two line black box, putting the list on the right is easy, and a separate issue. – David Carlisle Feb 9 at 16:34

A simple solution with a tabular environment and colortbl:

\documentclass[paper=letterpaper,fontsize=10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{geometry}
\usepackage{cabin}
\usepackage{fontawesome}
\usepackage{amsmath}
\usepackage{graphicx, array, multirow, makecell,}
\usepackage[svgnames, table]{xcolor}
\usepackage{url}
\frenchspacing
\pagestyle{empty}
\renewcommand\theadfont{\normalsize\bfseries\color{white}}

%%% BEGIN DOCUMENT ------------------------------------------------------------
\begin{document}

\sffamily
\begin{tabular} {>{\columncolor{black}}ccl}
& \faGithub & Github.com/Lastname\\
 & \faLinkedin & Linkedin.com/Lastname\\
\multirowthead{-3}{First name\\Last name} & \faMobile& 1234.5678
\end{tabular}

\end{document} 

enter image description here

share|improve this answer
    
Thank you; this seems like the easiest solution! I do wonder what causes the horizontal lines in the black box (at least for me, when I run lualatex on the .tex file) – LanneR Feb 9 at 5:54
    
For this very code? Or do you add \clines? That might explain. very thin white lines. – Bernard Feb 9 at 10:54

Perhaps I am mistaken that you are interested in multiple multiline coloured boxes placed adjacently. If that is not the case you can simply do it using parbox like this:

\documentclass{article}
\usepackage{color}    
\begin{document}

\colorbox{black}{\parbox[c]{6cm}{\centering
\color{white}\Huge\bfseries
First Name

Last Name
}}

\end{document}

enter image description here

If you want to place two such boxes adjacently such that they are of same height then you can do it using minipage and it is fairly simple:

\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{fontawesome}
\begin{document}

\noindent\colorbox{black}{%
\begin{minipage}[t][4cm][c]{0.3\textwidth}
\Huge \color{white} \bfseries
\centering

FirstName 

LastName

\end{minipage}}%
\noindent\colorbox{Crimson}{%
\begin{minipage}[t][4cm][c]{0.4\textwidth}
\centering
\color{BlanchedAlmond}\bfseries\large
    \begin{itemize}
        \item[\faGithub] GitHub
        \item[\faLinkedin] Linkedin/account
        \item[\faPhone] 123-456-7890
        \item[\faEnvelope] firstname@lastname.com
        \item[\faGlobe] www.firstname.com
    \end{itemize}
\end{minipage}}


\end{document}

With \begin{minipage}[t][5cm][c]{0.3\textwidth}, you can adjust the height and width of the coloured boxes (5cm and 0.3\textwidth respectively), as well as their alignments.

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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