Width of parbox inside colorbox

colorboxmarginparparbox

I want to insert a colorbox into a margin paragraph. parbox is used to keep the line breaks.

What value should I pass to \parbox's width so it doesn't get too wide as it does now?

colorbox

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{geometry,xcolor,calc}
\geometry{%
left=16mm,
textwidth=130mm,
marginparsep=8mm,
marginparwidth=50mm,
showframe}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
\marginpar{%
  \colorbox{orange}{%
    \parbox{\dimexpr\marginparwidth-2\fboxsep}{%
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.}
    }
  }%
ut labore et dolore magna aliqua.
\end{document}

The expression \dimexpr\marginparwidth-2\fboxsep is based on this answer but I don't see its effect.

Best Answer

You are adding spaces

...uat.}
    }
  }%
ut labore et dolore magna aliqua.
\end{document}

should be

uat.}%
    }%
  }%
ut labore et dolore magna aliqua.
\end{document}

a line end is a space compare

\fbox{\fbox{x}}

with

\fbox{x
 }
}

\fbox{x%
 }%
}
Related Question