0

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.

4
  • you have the right width but you have a space after the parbox inside the colorbox, add % after } and similarly you have a space after the coilorbox in tehmarginpar so add a second % Commented Feb 21, 2022 at 16:38
  • both \fbox{x } is not the same as \fbox{x} Commented Feb 21, 2022 at 16:40
  • Got you. That works.
    – tush
    Commented Feb 21, 2022 at 16:40
  • Would you like to post an answer, or shall I ask the moderator to close the question? If you could write down a sentence about the reason for why it happened, it would be very instructive.
    – tush
    Commented Feb 21, 2022 at 16:42

1 Answer 1

2

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%
 }%
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .