2

I'm writing a document where I need the following result:

enter image description here

I need also two more things:

  1. The box must adapt itself to the sentence, in this way when I write a short sentence, the box will not appear with any blank space.

enter image description here

  1. The code must allow the user to change the box width and not setting by default the entire line width. In this way larger sentences can be compressed as you like.

enter image description here

So I started looking for some options that help creating a box filled with text. I've come up with the following code lines:

\documentclass[11pt, a4paper]{report} 
\usepackage[utf8]{inputenc} 
\usepackage{microtype}
\usepackage{varwidth}

\begin{document}

\noindent\fbox{\begin{varwidth}{\linewidth}
    
        \textbf{Assumption}: This is the box where I would like that the text were aligned with the colon when it starts a new line.
        \end{varwidth}}

\end{document}

This code sample gives me the following result:

enter image description here

So, as the message in the box states, I would like to know how I could align the text likewise the first image. The actual code allows already to reduce the box width by just changing {\linewidth} with {4in} for example. The code is also capable of adapting the box and the frame to the sentence lenght without leaving blank parts and extra space, but the code didn't match the colon alignment I'm looking for. I hope you can help me in finding the best solution. If you have any suggestions about the code lines, or even a better strategy, I will appreciate.

Thank you very much.

2

EDIT after the OP clarified the intended output

\documentclass[twocolumn]{article}

\newcommand{\foo}[3][\linewidth]{%
   \par\noindent
   \sbox0{\fbox{\textbf{#2:}\quad#3}}%
   \ifdim\wd0<#1%
      \usebox0%
   \else
      \sbox0{\textbf{#2:}}%
      \fbox{%
         \copy0
         \quad
         \parbox[t]{\dimexpr#1-1em-\wd0-2\fboxsep-2\fboxrule\relax}{#3}%
      }%
   \fi
}

\begin{document}

Some random text just to show where the margins are.
Some random text just to show where the margins are.
Some random text just to show where the margins are.

\foo{Assumption}{This is a box.}

\foo{Assumption}{This is a longer box.}

\foo{Assumption}{This is a box almost one line long.}

\foo{Assumption}{This is a box more than one line long.}

\foo{Assumption}{This is the box where I would like the text
to be aligned with the colon when it starts a new line.}

\foo[.8\linewidth]{Example}{\raggedright An example with optional parameter
giving the maximal width, here set to \texttt{.8\string\linewidth}.}

\end{document}

enter image description here


ORIGINAL ANSWER

It's a bit of an overkill but you can (ab)use tabularx for this.

\documentclass[11pt, a4paper]{report} 

\usepackage{tabularx}

\begin{document}

\noindent
\begin{tabularx}{\linewidth}{|lX|}
\hline
\textbf{Assumption}: &
This is the box where I would like that the text were aligned with the colon
when it starts a new line.\\
\hline
\end{tabularx}

\end{document}

enter image description here

4
  • Beat me by 2 minutes (+1). One suggestion: {l@{ }X} – Steven B. Segletes Apr 7 at 17:32
  • @campa Thank you very much. I have specified more clearly the features I needed. I took for granted that for a matter of common taste was better to not leaving extra space in the event that the sentence would have been shorter. – Catarella Apr 8 at 11:18
  • @campa the code you provided works great this time. I really appreciate your contribution. Just one more thing. What kind of command should I put to get a good separation between the box and the following paragraph I write just outside and below it? I tried and I get the first sentence which is almost pasted to the box. I made an attempt with the command \newline after the box, but it adds too much space. – Catarella Apr 8 at 13:09
  • 1
    @Catarella You can add a \par\smallskip after \fi, though for symmetry I would add the spacing also above, i.e. in the first line of \foo write \par\smallskip\noindent. But this assumes that your box will always be on a line by itself. – campa Apr 8 at 13:17
1

A way to do that with the framed environment and enumitem. It can break across pages.

\documentclass[11pt, a4paper]{report}
\usepackage{microtype}
\usepackage{framed}
\setlength{\FrameSep}{4pt}
\usepackage{enumitem}
\usepackage{lipsum}

\begin{document}

\begin{framed}
  \begin{description}[widest = \textbf{Assumption:}, leftmargin=*, nosep]%
      \item[Assumption:] This is the box where I would like that the text were aligned with the colon when it starts a new line.
  \end{description}
\end{framed}
\lipsum[11]

\end{document} 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.