Makebox with text, stop text appearing on separate line

1,018

The color environment does not exist. While \begin{color} issues no error, the effects are quite unpredictable, as you discovered.

The solution is to put the color change in the box.

Don't use explicit font size or font family settings; the abstract commands are much better. Also giving colors a name is better, as it helps in abstracting things and in getting the same color where it should.

\documentclass{article}
\usepackage{xcolor}

\usepackage{showframe} % just for the example

\definecolor{cventry}{cmy}{0.7, 0.5, 0.3}

\begin{document}

\section{Wrong}

The following is wrong because the \texttt{color} environment
does not exist and using \verb|\begin{color}| leads to
unexpected results

\bigskip

\begin{color}[cmy]{0.7, 0.5, 0.3}
\noindent
\makebox[\linewidth][l]{\rule{2cm}{4pt}}
\fontsize{14}{0}\fontfamily{cmss}\selectfont\textbf{Experience}
\end{color}

\section{Right}

The solution is to put everything in the box.

\bigskip

\noindent\makebox[\linewidth][l]{%
  \color{cventry}%
  \Large\sffamily
  \rule{2cm}{4pt} \textbf{Experience}%
}

\end{document} 

enter image description here

The showframe package draws the borders of the text area; don't use it in a production version of the document.

Actually, the error is not due to the inexistent color environment, but to the fact that you put the rule in a \linewidth wide box, so the text necessarily went in the following line. But I preferred to emphasize the wrong usage of \color.

Share:
1,018

Related videos on Youtube

Conor
Author by

Conor

Updated on August 01, 2022

Comments

  • Conor
    Conor over 1 year

    I want to insert a rectangle followed by some text, e.g. if the dashes indicate the position of the rectangle, I want to achieve something like this:

    ---- some text

    I am using an article document class. I have tried:

    \begin{color}[cmy]{0.7, 0.5, 0.3}
    \noindent
    \makebox[\linewidth][l]{\rule{2cm}{4pt}}
    \fontsize{14}{0}\fontfamily{cmss}\selectfont\textbf{Experience}
    \end{color}
    

    This results in a blue rectangular box, but the text appears underneath, rather than adjacently. If I delete the newline between the \makebox and \fontsize lines, the text appears on the same line as the blue rectangle, but with a large amount of white space between.
    Any ideas would be greatly appreciated.

  • Bernard
    Bernard about 7 years
    However, I let the color environment on testing this, and it worked, as you can see from my answer. I don't understand very well why, but I think the main point was the text outside the \makebox[\linewidth]{}.
  • egreg
    egreg about 7 years
    @Bernard Yes, as the small text says. But it's wrong to use \begin{color} in the first place.
  • Bernard
    Bernard about 7 years
    Personnally, I never use it – just wanted to test.