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

Kayla
  • 1,655
  • 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 Nov 24 '12 at 17:39
  • May I ask why did you unaccept the answer? Is there something wrong with it? – Gonzalo Medina Nov 24 '12 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 Nov 24 '12 at 17:55
  • @Mohan: I would prefer the second option---> box which stretches to fit the width of the text I write. – Kayla Nov 24 '12 at 17:57
  • @kayla: then I see that your question has been answered below. – Mohan Nov 24 '12 at 17:59
  • Don't write \text here text here text here[2]. Write text here text here text here. The backslash indicates that \lipsum is a command and the [2] is an argument to that command. YOu don't need either of them. – Mohan Nov 24 '12 at 18:00
  • @kayla as I explained you, there's nothing wrong with my code and it works as expected; you had a syntax error in your replacement text. – Gonzalo Medina Nov 24 '12 at 18:01
  • it works now! :) But why is it when i write this continous word hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh the output is no longer the same? – Kayla Nov 24 '12 at 18:09
  • @kayla because that's not a "real" word, so LaTeX's algorithm for hyphenation can't find a place to break it appropriately. – Gonzalo Medina Nov 24 '12 at 18:10

2 Answers2

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}
Gonzalo Medina
  • 505,128
  • 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 Nov 24 '12 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]} – Gonzalo Medina Nov 24 '12 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 Nov 24 '12 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}. – Gonzalo Medina Nov 24 '12 at 18:03
  • @kayla please see my updated answer; there you can see that the code works as expected with your text. – Gonzalo Medina Nov 24 '12 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}

Peter Grill
  • 223,288