2

I am trying to define a new environment where the input text is placed inside a minipage, which has width equal to the length of the input text (something like a variable width box). What I would like to have additional is a horizontal line over the box which has length more than the input text (say 1.5*input-text-width), as shown below:

enter image description here

Though there are many methods to create a variable width box, I didn't see a way to draw a line over the box which which has its length connected to the input text. I believe that determining the length of the input text is the right way to proceed. In newcommand, the length can be easily determined, since the input text can be called as #1. My question is how to decide the input text length for a newenvironment. More exactly, where is the text-body defined inside the newenviroment?

A minimum working example is:

\documentclass{article}

\newenvironment{varlength}{
\begin{center}
\rule{0.9\textwidth}{0.8pt}

\begin{minipage}{0.6\textwidth}}{
\end{minipage}
\end{center}}


\begin{document}
\begin{varlength}
This is a text inside fixed length minipage with over-line

I want the same to be repeated for variable width box
\end{varlength}
\end{document}
  • Look for varwidth in the answers to the question marked as duplicate. – egreg Dec 1 '18 at 16:13
  • @egreg, thanks for the link. But in my environment, there is a line which spans over the text and has a length equal to 1.2 times the input-text-length. That's why I was asking for a way to find input length. I could not find a way to fix it in the link you shared. Should I edit my question? – Ashok Dec 1 '18 at 18:58
  • Sorry, but I don't understand. – egreg Dec 1 '18 at 20:40
  • @egred, I have edited my question, sorry for the delayed response. – Ashok Dec 4 '18 at 14:26
2

The “length of the input” doesn't make much sense, because TeX typesets paragraphs at a fixed size, using the current value of the parameter \hsize (normally equal to \textwidth in LaTeX).

You can have minipages that adapt to the size of the material inside them using varwidth:

\documentclass{article}
\usepackage{varwidth}

\newsavebox{\varlengthbox}
\newenvironment{varlength}
 {\begin{lrbox}{\varlengthbox}\begin{varwidth}{\textwidth}}
 {\end{varwidth}\end{lrbox}
  \begin{center}
  \rule{1.5\wd\varlengthbox}{0.4pt}\\
  \usebox{\varlengthbox}
  \end{center}}

\begin{document}

\begin{varlength}
This is a text inside fixed length\\
minipage with over-line

I want the same to be repeated for\\
variable width box
\end{varlength}

\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.