If you are satisfied with a plain box then this is probably not the solution for you. To illustrate that the basic case works:
But of course, being a tikz
solution you can style the boxes. Here are just a few of the many options that are avilaable:
Furthermore, by adding the option text width=\linewidth
, you can chose to have the box take up the full \linewidth
:
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}
\lipsum[2]
to \text here text here text here[2]`.. :(