7

1. Measuring total height of a paragraph :

Hello I'm trying to draw a black box of same height and width of a paragraph with several lines. For this I need to know the size of paragraph and I'm using the following function:

\documentclass{article}

\newlength{\mytextheight}
\newsavebox{\mytext}

\newcommand{\TestHeight}[1]
        {
                \savebox{\mytext}{#1}
                \settoheight{\mytextheight}{\usebox{\mytext}}
                {#1}
                Height of Text: \the\mytextheight\\
        }

\begin{document}
\TestHeight{This is sample text.}
\TestHeight{\Large This is larger text.}

\TestHeight{
This is sample text with multiple lines.\\
This is the second line.
The height is the same height as with one single line !! ?? \\
settoheight is very bad...
}
\end{document}

As you can see after testing, the height is not at all the good height (same heights for both single line and several lines). -> How to really measure this ?

2. Replacing the paragraph by a black rectangle of same dimensions at same position

After we got the good height how to create a tikz macro that replaces the text the paragraph by a black rectangle with exact the same dimensions (height, width) at the same position ? I tried this macro but according to what is explained above it obviously doesn't work as the total height is not good :

\usepackage{tikz} %to be used with package below:
\newlength\heighthide
\newlength\widthhide
\newcommand\hide[1]{%
 \settoheight{\heighthide}{#1}%
 \settowidth{\widthhide}{#1}%
 \tikz{\node[inner sep=0pt,rectangle,draw,
 text height=\heightconf,
 text width=\widthconf,
 fill=black]{};}
}

Thanks in advance for help.

  • See the censor package. Perhaps it might be what you are looking for (not for measuring, but for replacing text with black box). – Steven B. Segletes Aug 5 '14 at 9:47
  • @Steven B. Segletes: With multiple lines, the censor package dont work on this example (the black recangle has not the same size) : \documentclass{article} \usepackage{censor} \begin{document} There are several lines\\ Let's say 3 lines\\ I want to hide with \verb|censor| command.\\ \censor{ There are several lines\\ Let's say 3 lines\\ I want to hide with \verb|censor| command.\\} The black rectangle above has not the good dimensions\\ (multi lines not supported ?) \end{document} s1.postimg.org/d0avbkudr/censortest1.jpg and the proof here: writelatex.com/1321672jpytkm – jyloup Aug 5 '14 at 14:48
  • with \blackout i's a little bit better, but there is still alignment problems : \documentclass{article} \usepackage{censor} \usepackage{listings} \lstset{frame=single,escapechar=ç} \begin{document} There are several lines\\ Let's say 3 lines\\ I want to hide with censor command.\\ \blackout{ There are several lines\\ Let's say 3 lines\\ I want to hide with censor command.\\} \begin{lstlisting} a=b for =i do a=b end ç\blackout{for =i do a=b end}ç \end{lstlisting} The hidden words are not really at the good place. \end{document} writelatex.com/1321772hdzqfr – jyloup Aug 5 '14 at 15:19
  • Compare, for example, \documentclass{article} \usepackage{censor,lipsum} \begin{document} \setbox0=\vbox{\lipsum[1-2]}\box0 \clearpage\setbox0=\vbox{\lipsum[1-2]}\noindent\censorbox{\box0}\end{document}. I also see you trying to use it within listings, but I doubt that part of it will work. Also, I use a \vbox in this example, but an \hbox could be used if the item does not span the full \textwidth. – Steven B. Segletes Aug 5 '14 at 17:36
  • @ Steven Segletes: I found on this post (you commented it) : tex.stackexchange.com/questions/26283/… something very adapted for my need which seems to work well on both normal text and code listing (i tested it), but it replaces only by white area (due to phantom I think). How to make the hidden part black or another color ? About your comment above I really don't understand plain Tex language so it's not really clear for me at this time if there is no a complete testing code example. Thanks anyway for your help. – jyloup Aug 5 '14 at 20:22
4

The text in a \savebox is typeset all on one line. You need to use a \parbox inside \savebox.

There are also some problems in representing the real paragraph with a black box, the main one is to restore the correct depth for the interline spacing.

The example uses two columns just to see the effect side by side.

\documentclass{article}
\usepackage{multicol}

% in a \parbox the indent is set to zero, so we save it
\newlength{\saveparindent}
\AtBeginDocument{\setlength{\saveparindent}{\parindent}}

\newsavebox{\mytext}

\newcommand{\hide}[1]{%
  \savebox{\mytext}{%
    \parbox[t]{\columnwidth}{
      \setlength{\parindent}{\saveparindent}
      #1\par\xdef\savedprevdepth{\the\prevdepth}
    }%
  }%
  \noindent
  \vrule height \ht\mytext % the height of \mytext
         depth \dp\mytext  % the depth of \mytext
         width \columnwidth
  \par
  % restore \prevdepth to compute correctly the interline glue
  \prevdepth\savedprevdepth
}

\begin{document}
\begin{multicols}{2}
Some text.

A paragraph that in the next column will be hidden.

Another hidden paragraph.

Some text.

Some text.

\hide{A paragraph that in the next column will be hidden.

Another hidden paragraph.}

Some text.
\end{multicols}

\end{document}

enter image description here

  • +1 because (A) the interline spacing is properly solved and (B) the simply \vrule primitive instead tikz (suggested by OP) is used and (C) there is no csname with @. But I don't understand why didn't you use simply \vtop{#1\par\xdef\savedprevdepth{\the\prevdepth}} instead \parbox if there is \vrule primitive used immediately after it. The more complicated macro \parbox brings only problems: you need to save and restore \parindent. What happens when \parindent is changed after \begin{document}? Second: The usage of common \setbox0=\vtop... spares the box declaration. – wipet Aug 5 '14 at 11:13
  • Could you give an illustration code with your \vtop{#1\par\xdef\savedprevdepth{\the\prevdepth}}inside a listing code please ? That looks interesting. – jyloup Aug 5 '14 at 11:41
  • @jyloup Sorry, I am new at tex.sx.com and I don't know how to create listing inside comment. But you can start with egreg's code, remove \newlenght, \AtBeginDocument and \newsavebox lines and now you can replace \savebox{\mytext}{...} by \setbox0=\vtop{#1\par\xdef\savedprevdepth{\the\prevdepth}}. Finally you have to replace \ht\mytext and \dp\mytext by \ht0 and \dp0. – wipet Aug 5 '14 at 12:59
  • @wipet That would be possible, but \parbox does other things besides setting \parindent. On the other hand, \rule would be more complicated, using its optional arguments (and I didn't want to look up the manual ;-)). – egreg Aug 5 '14 at 13:36
  • @egreg I've taken a look to the latex.ltx to \parbox definition and I've found that \parbox really does other things, but none of them are usable. More precisely, they are disserviceable for this application. For example setting \baselineskip, \sloppy, ... They may cause that the paragraph inside \parbox is different than outside. – wipet Aug 6 '14 at 6:38

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.