6

I have following text chunk:

\colorbox{gray}{[862401.693536] sd 2:0:0:0: [sdb] Write Protect is off\hfill \break
[862401.693548] sd 2:0:0:0: [sdb] Mode Sense: 45 00 00 08\hfill \break
[862401.700884]  sdb: sdb1\hfill \break
[862401.705538] sd 2:0:0:0: [sdb] No Caching mode page present\hfill \break
[862401.705551] sd 2:0:0:0: [sdb] Assuming drive cache: write through\hfill \break
[862401.705562] sd 2:0:0:0: [sdb] Attached SCSI removable disk}

The output is visible in attached screenshot. Why \hfill and \break combo does not work?enter image description here

7
  • 1
    Can you extend that chunk into compilable code?
    – Johannes_B
    Mar 6, 2015 at 9:55
  • @Johannes_B, sorry, but I do not understand you. If you mean if I can compile code, then the answer is yes, no errors. Mar 6, 2015 at 9:56
  • 1
    You have a colorbox, something that looks like log-data, the biblatex tag. I mean, please provide a minimal working example.
    – Johannes_B
    Mar 6, 2015 at 9:57
  • @Johannes_B, this is minimal example. I just want to output some linux dmesg data in latex with grey background. Was the upper idea right way? Mar 6, 2015 at 10:00
  • 2
    When we ask for a MWE we ask for a piece of code that we can just copy and paste from the question and immediately compile on our machines and reproduce the issue. Such a MWE (almost always) contains a \documentclass statement and \begin{document}...\end{document}. What you posted can only be used to reproduce your problem if one is good at guessing certain things.
    – moewe
    Mar 6, 2015 at 10:14

3 Answers 3

8

Use the sledge hammer tcolorbox

\documentclass{article}

\usepackage{tcolorbox}

\begin{document}
\noindent
\begin{tcolorbox}[boxrule=0pt,arc=0pt,colback=gray]
[862401.693536] sd 2:0:0:0: [sdb] Write Protect is off\newline
[862401.693548] sd 2:0:0:0: [sdb] Mode Sense: 45 00 00 08\newline
[862401.700884]  sdb: sdb1\newline
[862401.705538] sd 2:0:0:0: [sdb] No Caching mode page present\newline
[862401.705551] sd 2:0:0:0: [sdb] Assuming drive cache: write through\newline
[862401.705562] sd 2:0:0:0: [sdb] Attached SCSI removable disk
\end{tcolorbox}
\end{document}

enter image description here

8

\colorbox is like \mbox and in such a box glue is set to its natural size, which is 0pt in the case of \hfill and \break sets a penalty for use in line breaking but no linebreaking happens in a horizontal box, so the penalty has no effect. If you want a paragraph inside an hbox you need

  \mbox{\parbox{5cm}{some text here}}

or the same with \mbox replaced by \colorbox{yellow}

\hfill\break should never be used in a latex document anyway, if you must force a break the latex syntax would be \\ but that similarly needs to be in a \parbox not an \mbox.

1
  • Hmm. This requires knowing the width of the box beforehand, though... doesn't that negate the purpose of the hfill? Or is there a simple way to make that parbox streatch to the end of the line?
    – Clément
    Aug 5, 2016 at 16:09
4

You probably want to do some highlighting with the soul package.

\documentclass{article}

\usepackage{soul,xcolor}

\begin{document}
\noindent
\sethlcolor{gray}\hl{[862401.693536] sd 2:0:0:0: [sdb] Write Protect is off\\
[862401.693548] sd 2:0:0:0: [sdb] Mode Sense: 45 00 00 08\\
[862401.700884]  sdb: sdb1\\
[862401.705538] sd 2:0:0:0: [sdb] No Caching mode page present\\
[862401.705551] sd 2:0:0:0: [sdb] Assuming drive cache: write through\\
[862401.705562] sd 2:0:0:0: [sdb] Attached SCSI removable disk}
\end{document} 

enter image description here

2
  • this is much better, but I want to gray section to be on whole line of text, i.e. rectangular. Mar 6, 2015 at 10:13
  • @MarkoFrelih in that case you want colorbox around a parbox, as in my answer:-) Mar 6, 2015 at 10:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .