10

I use makebox to create padded texts, however when the text in the box overflows, it overlaps with the following content. To demonstrate, enter image description here

The thing I want is a box that when the text is shorter than the given width, it works just as a normal makebox, however, when the text overflows, it should act as without a makebox, like the fourth line.

To be clear, what I want is something like:

\newcommand{flexbox}[2]{%
  \ifthenelse{\lessthan{\widthof{#2}}{#1}}{%
    \makebox{#1}[l]{#2}
  }{%
    #2
  }
}
shouya
  • 306
  • Welcome! I don't really know what you mean by behaving like the fourth line. Maybe you want a \parbox or minipage? Please give us the code for the output and not the output (or not only the output, at least). A small, complete document we can compile is best. – cfr Nov 01 '15 at 02:33
  • @cfr: Thank you! I've tried parbox, actually it behaves similarly when used inline. e.g. in \parbox{2em}{long long text} and else, "long long text" will still overlap with "and else". – shouya Nov 01 '15 at 02:49
  • Maybe something with tizs and minimum width? Going to bed now. – Dr. Manuel Kuehner Nov 01 '15 at 02:59

2 Answers2

10
\documentclass{article}
\newlength{\mylen}
\newcommand{\myflexbox}[2][3em]{%
  \settowidth{\mylen}{#2}%
  \ifdim\mylen < #1
    \makebox[#1][l]{#2}%
  \else
    #2%
  \fi
}
\begin{document}
  \myflexbox{good} padding

  \myflexbox{notbad} padding

  \myflexbox{it overlaps!} padding

  it overlaps! padding

\end{document}

enter image description here

Werner
  • 603,163
4

1st Try

Try the tabbing environment and next time please provide a complete example (including documentclass etc.)

The first line defines the tab position.

\documentclass{article}

\begin{document}

 \begin{tabbing}
    longestlongest \= long  \kill % kill -> do not print this line
    good \> padding \\
    notbad \> padding \\
    it overlaps \> padding \\
 \end{tabbing}

\end{document}

enter image description here


2nd Try

Now I use a tikz picture but I don't know how to left align (align=left) it without using text with. Maybe an expert can help us out.

\documentclass{article}
\usepackage{tikz}
\begin{document}

\parindent0mm

\section*{With Frame}   

\tikz[baseline=(O.base)]{\node(O) [baseline,draw,minimum width=15mm,inner sep = 0,align=left] {good};} padding\\
\tikz[baseline=(O.base)]{\node(O) [baseline,draw,minimum width=15mm,inner sep = 0,align=left] {notbad};} padding\\
\tikz[baseline=(O.base)]{\node(O) [baseline,draw,minimum width=15mm,inner sep = 0] {it does not overlap};} padding\\

\section*{No Frame (\texttt{draw} removed)} 

\tikz[baseline=(O.base)]{\node(O) [baseline,minimum width=15mm,inner sep = 0,align=left] {good};} padding\\
\tikz[baseline=(O.base)]{\node(O) [baseline,minimum width=15mm,inner sep = 0,align=left] {notbad};} padding\\
\tikz[baseline=(O.base)]{\node(O) [baseline,minimum width=15mm,inner sep = 0] {it does not overlap};} padding\\

\end{document}

I used this question for help.

enter image description here

  • Thank you, this environment is cool! Hoever, I wanted to have multiple makeboxes inline and this environment doesn't seem to work with it. – shouya Nov 01 '15 at 02:41