10

I have a problem on how I can make my command right. I created a box where the text will be inside it when used but something went wrong when I prolong the text it turn out like this:

Code:

\newcommand{\casebox}[1]{%
\framebox[.5\textwidth][t]{#1}
}

enter image description here

I hope I can make something like this by my command:

enter image description here

10
  • Do you want to have a box with a specified fixed width and have the text wrap inside that box? Or do you want a box that stretches to fit the width of the text you write?
    – Mohan
    Commented Nov 24, 2012 at 17:39
  • May I ask why did you unaccept the answer? Is there something wrong with it? Commented Nov 24, 2012 at 17:50
  • i feel sorry to accept the answer thoroughly. The thing is, it succesfully compiled but the error is the same when i replace \lipsum[2] to \text here text here text here[2]`.. :(
    – Kayla
    Commented Nov 24, 2012 at 17:55
  • @Mohan: I would prefer the second option---> box which stretches to fit the width of the text I write.
    – Kayla
    Commented Nov 24, 2012 at 17:57
  • @kayla: then I see that your question has been answered below.
    – Mohan
    Commented Nov 24, 2012 at 17:59

2 Answers 2

10

A possible solution using the varwidth package:

\documentclass{article}
\usepackage{varwidth}
\usepackage{lipsum}% just to generate text for the example

\newcommand\mybox[2][\dimexpr\textwidth-2\fboxsep\relax]{%
\fbox{\begin{varwidth}{#1}
#2
\end{varwidth}}}

\begin{document}

\noindent\mybox{test}

\noindent\mybox{some longer text}

\noindent\mybox{text here text here text here text here text here}

\noindent\mybox{\lipsum[2]}

\end{document}

enter image description here

If my command is always meant to start and end its own paragraph, then the definition can be changed to something like

\newcommand\mybox[2][\dimexpr\textwidth-2\fboxsep\relax]{%
\par\noindent\fbox{\begin{varwidth}{#1}
#2
\end{varwidth}}\par}

and then one can simply say

\mybox{text text text text text}
5
  • I can still encounter the same problem. When I replace the command \noindent\mybox{\lipsum[2]} to \noindent\mybox{text here text here text here text here text here[2}} Still the same output.. some text are outside the box.
    – Kayla
    Commented Nov 24, 2012 at 17:51
  • @kayla \noindent\mybox{text here text here text here text here text here[2}} has a syntax error; there's an additional brace at the end. Try \noindent\mybox{text here text here text here text here text here[2]} Commented Nov 24, 2012 at 17:53
  • i already compiled this \noindent\mybox{text here text here text here text here text here[2]} and it do gave me no error. But, the output is the same. Half of the text is outside the box. What should i do? the argument [2] can visibly seen at the end of the text.
    – Kayla
    Commented Nov 24, 2012 at 18:01
  • 1
    @kayla if you don't need [2], then don't write it; I used it because it was part of the \lipsum command to automatically generate text; \lipsum[2] means the second text paragraph. You don't really need this; use simply \noindent\mybox{text text text text text text text text text text}. Commented Nov 24, 2012 at 18:03
  • @kayla please see my updated answer; there you can see that the code works as expected with your text. Commented Nov 24, 2012 at 18:09
6

If you are satisfied with a plain box then this is probably not the solution for you. To illustrate that the basic case works:

enter image description here

But of course, being a tikz solution you can style the boxes. Here are just a few of the many options that are avilaable:

enter image description here

Furthermore, by adding the option text width=\linewidth, you can chose to have the box take up the full \linewidth:

enter image description here

Code:

\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}
\usepackage{lipsum}

\newlength{\LengthOfText}
\newlength{\LengthOfTextExceedingLineWidth}
\newlength{\TextWidth}
\newcommand{\Boxed}[2][]{%
    % #1 = box draw/fill options
    % #2 = text
    \settowidth{\LengthOfText}{\mbox{#2}}%
    \pgfmathsetlength{\LengthOfTextExceedingLineWidth}
        {\LengthOfText-\linewidth}
    \pgfmathsetlength{\TextWidth}{\LengthOfTextExceedingLineWidth > 0pt ? \linewidth : \LengthOfText}%
    \begin{tikzpicture}[baseline, inner sep=2pt, outer sep=0]
            \node [, text width=\TextWidth, #1] (Origin) {#2};
            \draw [thick, draw=black, #1] 
                (Origin.south west) rectangle (Origin.north east) ;
    \end{tikzpicture}%
}


\begin{document}
\noindent\Boxed{test}\par\medskip\noindent
\noindent\Boxed{some longer text}\par\medskip\noindent
\noindent\Boxed{text here text here text here text here text here}\par\medskip\noindent
\noindent\Boxed{\lipsum[2]}

\bigskip\noindent
\Boxed[red]{test}\par\medskip\noindent
\Boxed[dashed]{text here text here text here text here text here}\par\medskip\noindent
\Boxed[fill=yellow!20, fill opacity=0.3, text opacity=1]{\lipsum[2]}

\bigskip\noindent
\Boxed[blue, text width=\linewidth]{test}\par\medskip\noindent
\Boxed[draw=brown, text width=\linewidth]{text here text here text here text here text here}\par\medskip\noindent
\end{document}

You must log in to answer this question.

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