Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is it possible to create a new command of the form ----------{TEX}----------?

For instance, I would like to have something like \nc{TEX} which produces "TEX" in the middle of the line and the rest of it should be filled with '-'.

\nc{TEX} -> ----TEX----

share|improve this question
    
The rules at the side are solid or dashed? –  Gonzalo Medina May 20 at 23:17
    
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. –  Heiko Oberdiek May 20 at 23:38

5 Answers 5

up vote 22 down vote accepted

If you want more flexibility you can use tikz:

enter image description here

Code:

\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand{\nc}[2][]{%
\tikz \draw [draw=black, ultra thick, #1]
    ($(current page.center)-(0.5\linewidth,0)$) -- 
    ($(current page.center)+(0.5\linewidth,0)$)
    node [midway, fill=white] {#2};
}

\begin{document}

\nc{\TeX}

\nc[dashed]{\TeX}

\bigskip

\nc[draw=red]{$\displaystyle\sum_{i=0}^n i^2=\frac{1}{3}n\left(n+\frac{1}{2}\right)(n+1)$}

\nc[draw=blue, dotted]{$\displaystyle\sum_{i=0}^n i^2=\frac{1}{3}n\left(n+\frac{1}{2}\right)(n+1)$}

\end{document}
share|improve this answer

With this macro the rule is automatically vertically centered with respect to the size of the material. The optional argument is for the rule thickness (default 0.4pt, the standard size of rules).

\documentclass{article}

\newcommand{\nc}[2][0.4pt]{%
  \sbox0{#1}%
  \par\noindent
  \makebox[\textwidth]{%
    \leaders\hrule height\dimexpr(\ht0-\dp0)/2+(#1)/2\relax
                   depth-\dimexpr(\ht0-\dp0)/2-(#1)/2\relax
    \hfill
    \ #2\ %
    \leaders\hrule height\dimexpr(\ht0-\dp0)/2+(#1)/2\relax
                   depth-\dimexpr(\ht0-\dp0)/2-(#1)/2\relax
    \hfill
  }%
  \par
}

\begin{document}

\nc{\TeX}

\bigskip

\nc[1pt]{$\displaystyle\sum_{i=0}^n i^2=\frac{1}{3}n\left(n+\frac{1}{2}\right)(n+1)$}

\end{document}

enter image description here

share|improve this answer

The optional argument for \NC allows you to control the rule thickness:

\documentclass{article}%
\usepackage{xhfill}

\newcommand\NC[2][0.4pt]{%
  \noindent\xrfill[0.65ex]{#1}~{#2}~\xrfill[0.65ex]{#1}}

\begin{document}

\NC{\TeX}

\NC[1pt]{\TeX}

\NC{Some other text}

\NC[1pt]{Some other text}

\end{document}

enter image description here

share|improve this answer

Here is a possible solution:

\documentclass{article}

\newcommand*\NiceLine{%
  \par\noindent\raisebox{0.8ex}{\makebox[\linewidth]{\hrulefill\hspace{1ex}\raisebox{-0.8ex}{\TeX}\hspace{1ex}\hrulefill}}}

\begin{document}

\NiceLine

\end{document}

output

share|improve this answer

Lest you wanted dashes (or not):

\documentclass{article}

\usepackage{calc, dashrule}

\newcommand*{\nc}[1]{%
  \par\noindent\hrulefill#1\hrulefill\par}
\newlength{\mcw}
\newlength{\mcwidth}
\newcommand*{\mc}[1]{%
  \settowidth{\mcw}{#1}%
  \setlength{\mcwidth}{.5\textwidth-.5\mcw}%
  \par\noindent\rule[.5ex]{\mcwidth}{.4pt}#1\rule[.5ex]{\mcwidth}{.4pt}\par}
\newcommand*{\qc}[1]{%
  \settowidth{\mcw}{#1}%
  \setlength{\mcwidth}{.5\textwidth-.5\mcw}%
  \par\noindent\hdashrule[.5ex][x]{\mcwidth}{.4pt}{2mm 1pt}#1\hdashrule[.5ex][x]{\mcwidth}{.4pt}{2mm 1pt}\par}

\begin{document}

\nc{TEX}
\mc{TEX}
\qc{TEX}

\end{document}

for baseline, mid and dashed rules:

Low rule, mid rule, dashed rule

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.