# How to align text in title with \fcolorbox

I have defined a new command to make some titles for a newsletter. I'm using the article class for the document. The command is defined with the following code:

\definecolor{myblue}{rgb}{0.16, 0.32, 0.75}
\newcommand{\articletitle}[1]
{
\stepcounter{art}
\setcounter{section}{0}
\vspace{0.15cm}
{\raggedright\fcolorbox{myblue}{myblue}{{\huge \textcolor{white}{\arabic{art}}}}\hspace{1em}\textcolor{myblue}{\huge{#1}}}\\
\rule{\textwidth}{1pt}
\vspace{-0.25cm}
}


Which gives the expected result:

The problem is when the title is larger than one line, which gives:

I would like the second line of the title to be aligned with the first letter with the same space than between the first letter and the \fcolorbox.

The result should look like this:

I have searched how to do this without success. Of course I'm sure this is pretty easy to do with LateX! So if someone can help me with this...

Thanks

-
Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – Christian Hupfer Jun 3 '14 at 17:14
There are various considerations that you need to specify: 1) Do you require fully aligned (vs. raggedright) titles when they are multi-line? 2) Have you made provisions for adding this content to a table of contents (various solutions may inhibit that process)? 3) Is requiring you to manually specify a line break OK or must the solution do it automatically?, etc. [I have a solution ready to go that is raggedright when multiline, toc inhibited, and requires manual line breaks, but I don't know if that is acceptable or not] – Steven B. Segletes Jun 3 '14 at 17:24
I need the second line to be fully aligned with the first letter of the first line (left aligned). I don't need the title to be in a TOC. I could manually specify a line break but it would be great if the code can do this automatically. – Xavier Jun 3 '14 at 17:33

Here is a solution using tabularx. I think that for hanging titles, it is better to have them ragged right:

\documentclass[a4paper,twoside,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc} %
\usepackage{helvet}
\renewcommand\thepage{{\sffamily\arabic{page}}}

\usepackage{tabularx}
\usepackage{xcolor}

\newcounter{art}[section]
\setcounter{art}{0}
\definecolor{myblue}{rgb}{0.16, 0.32, 0.75}
\newcommand\labelart{\raisebox{-\height}{\fcolorbox{myblue}{myblue}{\centering\textcolor{white}{\,\arabic{art}\,}}}}
\newcommand{\articletitle}[1]
{%
\stepcounter{art}
\setcounter{section}{0}
\vspace{0.15cm}
{\LARGE\noindent\begin{tabularx}{\linewidth}{@{}l @{\enspace}>{\raggedright\color{myblue}}X@{}}
\labelart & #1
\end{tabularx}}\\
\textcolor{myblue}{\rule{\linewidth}{1pt}}
\vspace{-0.25cm}
}

\usepackage{lipsum}

\begin{document}
\sffamily
\let\defaultfamily\sffamily

\articletitle{This is a very very long title which is properly aligned}

\lipsum[1-2]

\articletitle{This is a shorter title}

\lipsum[3-4]

\end{document}


-
Be cautious of page breaks immediately after an \articletitle. – Werner Jun 3 '14 at 23:39
A great solution that exactly does what I need. I would not have thought to use a tabularx here. Thanks! – Xavier Jun 3 '14 at 23:57

With titlesec:

\documentclass[a4paper,twoside,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc} %
\usepackage{helvet}
\renewcommand\thepage{{\sffamily\arabic{page}}}

\usepackage{titlesec}
\usepackage{xcolor}
\definecolor{myblue}{rgb}{0.16, 0.32, 0.75}

\titleformat{\section}
\normalfont\sffamily}
{\fcolorbox{myblue}{myblue}{\textcolor{white}{\thesection}}}
{1em}
{}
%[\vspace{1ex}\titlerule]
[\vspace{-1ex}\rule{\linewidth}{1pt}]

\titlespacing{\section}
{0pc}{*2}{*1}[0pc]

\usepackage{lipsum}

\begin{document}
\sffamily
\let\defaultfamily\sffamily

\section{This is a very very long title which is properly aligned}

\lipsum[1-2]

\section{This is a shorter title}

\lipsum[3-4]

\end{document}


-
Thanks. This is a great solution, but I don't like much the hyphenation of the title. Is there a way to avoid this? – Xavier Jun 3 '14 at 23:50
@Xavier Add \raggedright. I have updated the answer. – Harish Kumar Jun 3 '14 at 23:54
This is a great solution. I was using the \section for the article sections, but I can start using a \subsection in the article. Thanks! – Xavier Jun 4 '14 at 0:02

Here is a solution with some drawbacks, though it is not clear if that is acceptable to the OP or not. The drawbacks:

1) multiline article titles are ragged right.

2) line breaking must be manually specified

3) because of (2), special provisions would be required to strip line breaks before adding the argument to a table of content.

The approach places the article title into a left-aligned \Longunderstack, which may contain manual \\ linebreaks. The interline spacing of the titles are currently set to 0.8\baselineskip but can be modified in the preamble.

\documentclass{article}
\usepackage{xcolor, lipsum}
\usepackage[usestackEOL]{stackengine}
\newcounter{art}
\definecolor{myblue}{rgb}{0.16, 0.32, 0.75}
\newcommand\articletitle[1]
{
\stepcounter{art}
\setcounter{section}{0}
\vspace{0.15cm}
{\raggedright\fcolorbox{myblue}{myblue}{{\huge \textcolor{white}%
{\arabic{art}}}}\hspace{1em}\textcolor{myblue}{\huge{%
\Longunderstack[l]{#1}}}}\\
\rule{\textwidth}{1pt}
\vspace{-0.25cm}
}
\setstackgap{L}{.8\baselineskip}
\begin{document}
\sffamily
\articletitle{This is a test title}

\lipsum[1]

\articletitle{
This is a very long title for the line \\
length which is not properly aligned}

\lipsum[1]
\end{document}


-
Thanks. This is an acceptable solution, even if not automatic. It is much better than my ugly previous title! :-) – Xavier Jun 3 '14 at 17:40