Position image next to a text within an adjustbox created in a newenvironment
I thought I would show you the lettrine approach.
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[british]{babel}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\definecolor{lightgray}{HTML}{f5f5f5}
\usepackage{adjustbox}
\usepackage{fontawesome}
\usepackage{lettrine}
\newenvironment{idea}
{%
\noindent\begin{adjustbox}{minipage=\linewidth,margin=1ex,bgcolor=lightgray,env=center}
\lettrine{\faCoffee}{}%
}
{%
\end{adjustbox}%
}
\begin{document}
\begin{idea}
I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works.
\end{idea}
\end{document}
This version centers the coffee cup (more or less) with the text.
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[british]{babel}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\definecolor{lightgray}{HTML}{f5f5f5}
\usepackage{adjustbox}
\usepackage{fontawesome}
\usepackage{lettrine}
\newenvironment{idea}[1]% #1 = text on left
{\noindent\begin{adjustbox}{minipage=\linewidth,margin=1ex,bgcolor=lightgray,env=center}
\sbox0{#1}% measure width
\usebox0\hfill% align baseline with center of minipage
%\raisebox{\dimexpr 0.5\dp0-0.5\ht0}{\usebox0}\hfill% align center with center
\begin{minipage}{\dimexpr \textwidth-\wd0-\columnsep}% added \columnsep gap between
}
{%
\end{minipage}\end{adjustbox}%
}
\begin{document}
\begin{idea}{\Huge \faCoffee}
I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works.
\end{idea}
\end{document}
I propose a solution based on the nice package xcoffin
.
As I explained earlier I found many advances using xcoffin
for cover design, title page and similar tasks, when the problem is typeset graphic material, tables or text in precise places of the page and/or relative positions between them.
Please see my answer to Create a box using the tcolorbox package or any other? (image)
This case is solved using four coffins. The Output will collect the three others and be inserted (typeset) at the text insertion point.
The icon is in the SideIcon coffin. The text is in the WarningText coffin. It will be loaded by the environment. Its width will be adjusted according to the SideIcon width plus some margin.
The BackFrame coffin is a gray rectangle with its height adjusted to the resulting height of the WarningText coffin, plus some margin.
First we join the BackFrame to the Output; next we join the SideIcon left-side-center to the left-side-center of BackFrame.
At last the WarningText left-side-vertical-center is joined to the right-side-vertical-center of the SideIcon. So the text will be always centered with the icon, with any font size or number of lines.
After typesetting the Output coffin, some vertical space is added to separate the next paragraph.
The code is simple and (almost) auto documented: 4 settings, 3 joins, 1 typeset.
In any case the reference manual of xcoffin
is well written and short. Only few commands are provided and fewer are needed for the common tasks.
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc} % not used here
\usepackage[british]{babel} % not used here
\usepackage[T1]{fontenc}% not used here
\usepackage{adjustbox} % not used here
\usepackage{lettrine} % not used here
\usepackage{xcolor}
\definecolor{lightgray}{HTML}{f5f5f5}
\usepackage{fontawesome}
\usepackage{xcoffins,calc,xparse} % needed
\usepackage{lipsum} % only for this example
\NewCoffin\Output %Coffin to hold the others
\NewCoffin\WarningText % Definition definition ...
\NewCoffin\BackFrame % Background: light gray rectangle
\NewCoffin\SideIcon %lateral left border
\NewDocumentEnvironment{idea}{+b}{%
\SetHorizontalCoffin\Output{} % It will provide the reference point join the others
\SetHorizontalCoffin\SideIcon{\Huge \faCoffee}
\SetVerticalCoffin{\WarningText}{\linewidth- \CoffinWidth\SideIcon-3em}{\noindent #1}
\SetHorizontalCoffin\BackFrame{\color{lightgray}\rule{\linewidth}{\CoffinTotalHeight\WarningText + \baselineskip}}
\JoinCoffins*\Output[l,t]\BackFrame[l,t] %attach left-top corner of BackFrame to idem of Output
\JoinCoffins*\Output[\BackFrame-l,\BackFrame-vc]\SideIcon[l,vc](1em,0pt) %attach leftside center of SideIcon to idem of Output
\JoinCoffins*\Output[\SideIcon-r,\SideIcon-vc]\WarningText[l,vc](1em,0pt) %attack left-top corner of WarningText to idem of Output
% Typeset Output
\noindent\TypesetCoffin\Output % at the text insertion point. It is not a float.
\vspace*{\CoffinTotalHeight\WarningText}\bigskip\bigskip %make some room for Output
}{}
\begin{document}
\lipsum[1][3-4]
\begin{idea}
I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works.
\end{idea}
\lipsum[2]
\begin{idea}
I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works.
\end{idea}
\begin{idea}
I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works.
\end{idea}
\lipsum[1][3-4]
\begin{idea}
\Large I hope it works. I hope it works. I hope it works. I hope it works.
\end{idea}
\end{document}
Boxes and save boxes!
\documentclass{article}
\usepackage{xcolor}
\usepackage{fontawesome}
%% just for the example
\usepackage{showframe}
\renewcommand*\ShowFrameLinethickness{.1pt}
%%
\definecolor{lightgray}{HTML}{f5f5f5}
\newsavebox{\ideabox}
\newlength{\ideawd}
\setlength{\ideawd}{1cm}
\newenvironment{idea}[1]
{%
\par\addvspace{\topsep}%
\begin{lrbox}{\ideabox}%
\makebox[\ideawd][l]{\begin{tabular}{@{}c@{}}\huge#1\end{tabular}}%
\begin{minipage}{\dimexpr\textwidth-2\fboxsep-\ideawd}%
}
{%
\end{minipage}\end{lrbox}%
\noindent\colorbox{lightgray}{\usebox{\ideabox}}%
\par\addvspace{\topsep}%
}
\begin{document}
\begin{idea}{\faCoffee}
I hope it works. I hope it works. I hope it works. I hope it works.
I hope it works. I hope it works. I hope it works. I hope it works.
I hope it works. I hope it works. I hope it works. I hope it works.
I hope it works. I hope it works.
\end{idea}
\begin{idea}{\faAutomobile}
I hope it works. I hope it works. I hope it works. I hope it works.
I hope it works. I hope it works. I hope it works. I hope it works.
I hope it works. I hope it works. I hope it works. I hope it works.
I hope it works. I hope it works.
\end{idea}
\end{document}
With showframe
we can see that the color box fits the textwidth exactly.
It also works with shorter ideas.