I created an MWE-look-alike in the original document then 'background highlighted' (somehow 'highlight' means something else in verbatim) some text to show where/what to focus on. This MWE-look-alike is targetted at those not familiar at all with LaTeX.
The verbatim text was produced using the listings
package, and the highlight was produced using custom code from here: https://tex.stackexchange.com/a/49309, tikz
, and xcolor
. The problem with this custom code, however, is line breaks.
The MWE-look-alike in the original document looks something like this at 100% zoom:
In the screenshot I highlighted four lines:
- Line 1: Whole line.
- Line 2: Partial line.
- Line 3: Code with empty space before the highlight.
- Line 4: Code inline with text.
Line 1 and 2 are OK. Line 3 and 4 are problematic.
Line 3 and 4 are too long that they extend beyond the margin. They cannot respect the margin because they cannot line break. Line 3 also does not respect the empty space before the highlighted text.
My question is:
- Can anyone help unhighlight the empty space before the highlighted text in Line 3?
- Can anyone help with the long lines/ line breaks problem for this particular code?
It has been many years since the custom code was written, so I wonder if anyone is(/has been) able to solve this line break problem. I can't do it because I don't know how.
The reason I chose this custom code is:
- It preserves the 'language highlight' (whereas
\colorbox
removes the language highlight), - I don't have to change the backslash character "
\
" to{\char92}
, and - It's cleaner to use because the customizing is done in the preamble (which is less cluttery than customizing in the verbatim text itself).
I really want this custom code to work because it's really good. Can anyone help improve the code, please.
This is the MWE:
\documentclass{scrbook}
\usepackage{tikz}
\usepackage{listings}
\lstset{%
language=[LaTeX]TeX,
commentstyle=\color{gray},
breaklines=true,
frame=single,
frameround=tttt,%
framextopmargin=10pt,
framexbottommargin=7pt,
xleftmargin=22pt,
framexleftmargin=10pt,
framesep=10pt
}
% - - - - - - - - - - - - - % Code for highlighting.
\makeatletter%
\newenvironment{btHighlight}[1][]%
{\begingroup\tikzset{bt@Highlight@par/.style={#1}}\begin{lrbox}{\@tempboxa}}%
{\end{lrbox}\bt@HL@box[bt@Highlight@par]{\@tempboxa}\endgroup}
\newcommand\btHL[1][]{%
\begin{btHighlight}[#1]\bgroup\aftergroup\bt@HL@endenv%
}
\def\bt@HL@endenv{%
\end{btHighlight}%
\egroup%
}
\newcommand{\bt@HL@box}[2][]{%
\tikz[#1]{%
\pgfpathrectangle{\pgfpoint{1pt}{0pt}}{\pgfpoint{\wd #2}{\ht #2}}%
\pgfusepath{use as bounding box}%
\node[anchor=base west, fill={oposmy!30},outer sep=0pt,inner xsep=1pt, inner ysep=0pt, minimum height=\ht\strutbox+1pt,#1]{\raisebox{1pt}{\strut}\strut\usebox{#2}};%
}%
}%
\makeatother%
\lstdefinestyle{SQL}{%
moredelim=**[is][\btHL]{`}{`},%
moredelim=**[is][{\btHL[fill=green!30,draw=red,dashed,thin]}]{@}{@}%
}
% - - - - - - - - - - - - - %
\usepackage{xcolor}
\definecolor{oposmy}{HTML}{c0f1ff}
\begin{document}
\begin{lstlisting}[style=SQL]
\documentclass{scrbook}
`\usepackage{fontspec`}% #1
\setmainfont{`Latin Modern Sans Demi Cond`}% #2
\usepackage{tikz}
\usetikzlibrary{shapes.misc}
\begin{document}
\begin{tikzpicture}
`\node[draw,rounded rectangle, rounded rectangle arc length=180] {Text};`% #3
\end{tikzpicture}
Press the `F1` key for help. Press the `\tikz[baseline] \draw node[anchor=base,draw,double,rounded corners] {{\texttt{F11}}};` key for full screen.% #4
\end{document}
\end{lstlisting}
\end{document}