Updated
A TikZ solution.
The implementation relies on a pair of commands \open
and \close
for the HTML syntax (and \openvar
, \closevar
for the other syntax). The commands are defined as follows:
\newcommand\open[1]{
\tikz[remember picture,baseline=(open#1.base)]{\node(open#1){<#1>};}
}
\newcommand\close[3][red]{
\tikz[remember picture,baseline=(close#2.base)]{
\node(close#2){</#2>};
\draw[#1,overlay](open#2)to[ncbar=#3](close#2);
}
}
The -var
version of the commands for the second syntax are similarly defined.
The \open
command takes one argument, i.e. the name of the open tag. The \close
command takes 3 arguments:
- the first optional argument sets the color of the "bracket" connected the open and close tags (with default set to
red
);
- the second argument is the name of the close tag;
- the third argument determines the height of the "bracket". Note that the bracket is implemented by the
ncbar
style, provided by Jake's answer here.
Full code
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
% ncbar style - https://tex.stackexchange.com/a/55069/18228
\tikzset{
ncbar angle/.initial=90,
ncbar/.style={
to path=(\tikztostart)
-- ($(\tikztostart)!#1!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztotarget)$)
-- ($(\tikztotarget)!($(\tikztostart)!#1!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztotarget)$)!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztostart)$)
-- (\tikztotarget)
},
ncbar/.default=0.5cm,
}
\tikzset{every node/.style={inner sep=0pt, outer sep=0pt}} % remove unnecessary space
% HTML syntax
\newcommand\open[1]{
\tikz[remember picture,baseline=(open#1.base)]{\node(open#1){<#1>};}
}
\newcommand\close[3][red]{
\tikz[remember picture,baseline=(close#2.base)]{
\node(close#2){</#2>};
\draw[#1,overlay](open#2)to[ncbar=#3](close#2);
}
}
% second 'unknown' syntax
\newcommand\openvar[1]{
\tikz[remember picture,baseline=(open#1.base)]{\node(open#1){<\uppercase{#1}>};}
}
\newcommand\closevar[3][red]{
\tikz[remember picture,baseline=(close#2.base)]{
\node(close#2){</>};
\draw[#1,overlay](open#2)to[ncbar=#3](close#2);
}
}
\begin{document}
\ttfamily
\open{b} nur fett \open{i} fett kursiv \close[green]{b}{.5cm} nur kursiv \close{i}{.7cm}
\bigskip\bigskip
\openvar{b} nur Fett \openvar{i} fett kursiv \closevar{i}{.5cm}\closevar[green]{b}{.7cm}\openvar{i} nur kursiv \closevar{i}{.5cm}
\end{document}

Update 2
A slightly more efficient way of defining the \close
command, which has only two arguments:
\newcommand\close[2][red]{
\tikz[remember picture,baseline=(close#2.base)]{
\node(close#2){</#2>};
\draw[ncbar,#1,overlay](open#2)to(close#2);
}
}
Now it only takes two arguments:
The first, optional argument controls the line style of the "bracket". The default is currently set to red
, and maybe changed to [red,thick]
to produce a red and thick line if the optional argument is not given.
- Note also that the
ncbar
option is now given to the \draw
operation, so by default, the height of the bracket is .5cm
. If a bracket of height .7cm needs to be drawn, just use \close[ncbar=.7cm,<other options>]{<tag name>}
.
The second argument is the same as before: the name of the close tag.
With \closevar
similarly modified, the above two examples can be implemented as
\open{b} nur fett \open{i} fett kursiv \close[green]{b} nur kursiv \close[red,ncbar=.7cm]{i}
and
\openvar{b} nur Fett \openvar{i} fett kursiv \closevar{i}\closevar[green,ncbar=.7cm]{b}\openvar{i} nur kursiv \closevar{i}