1

I'm creating a tex file from markdown via pandoc, and to overcome the inherent markup limitations I thought I'd try to redefine the \emph and \textbf macros in the latex template, to enable finer control over the formatting. For instance, in a specific section of the document I'd like **text** to become {\bfseries\scshape text}. It works well enough, but somehow \renewcommand has a very limited scope in a tabular environment: I end up having to add it to every single line. Is this the normal behaviour? Is there a workaround to expand the scope to the whole tabular environment?

\documentclass{article}
\newcommand{\fb}[1]{{\scshape#1}}

\begin{document}
\begin{tabular}{l}
\textbf{a}a\\
\renewcommand*{\textbf}[1]{\fb{#1}}\textbf{a}a\\
\textbf{a}a\\
\textbf{a}a
\end{tabular}
\end{document}

Note: I'm aware that I could simply use raw tex markup in the markdown source, but I'd like to avoid this alternative strategy in the interest of writer-oriented legibility.

  • it is not wise to redefine latex commands/macros. rather define new one. otherwise you will have awful mess i your code. and don't make redefinition in document. let be collected in preamble. otherwise you will have a lot of problems sooner or later. – Zarko Mar 11 '18 at 20:15
  • yes, I know it runs against all the rules. But markdown syntax has its appeal, and I prefer to have a clean text as input at the cost of an "awful mess" in the template, rather than the opposite. The alternative, keeping the writer happy, would be a convoluted AST filter that replaces ** ** with a user-defined macro \mytextbf{} that I would overwrite instead of \textbf{}. So far I haven't seen any reason to introduce this additional step (I will if I notice unexpected problems with formatting). – user156626 Mar 11 '18 at 20:55
0

All cells hold their contents local. \global defines it globally:

\documentclass{article}
\newcommand\fb[1]{{\scshape#1}}

\begin{document}
\begin{tabular}{l}
\textbf{a}a\\
\global\let\textbf\fb  \textbf{a}a\\
\textbf{a}a\\
\textbf{a}a
\end{tabular}
\end{document}

However, it is not a good idea to overwrite existing LaTeX macros

  • Thanks, it does extend the scope, but a little too much as it bleeds outside tabular. I could of course undo this at the end of tabular with \global\let\textbf\oldtextbf (saved earlier), but out of curiosity is there a way to restrict the scope? – user156626 Mar 11 '18 at 20:50
0

As Zarko said, it's best not to mess with standard latex macros if you can, however, I realise that you sometimes have no choice if you want to modify automatically produced output, such as the output from pandoc.

I am not sure if this is an option but the easiest way is to use a custom tabular environment that "locally" redefines \textbf for that environment. Using this approach, here's your modified MWE:

\documentclass{article}
\newcommand{\fb}[1]{{\scshape#1}}

\newenvironment{Tabular}[1]%
  {\renewcommand*{\textbf}[1]{\fb{##1}}\tabular{#1}}%
  {\endtabular}

\begin{document}
  \begin{Tabular}{l}
    \textbf{a}a\\
    \textbf{a}a\\
    \textbf{a}a
  \end{Tabular}

  \textbf{a}
\end{document}

which produces the expected

enter image description here

To apply this to all tabular environments with minimal effort you could use:

\documentclass{article}
\newcommand{\fb}[1]{{\scshape#1}}

\let\realTabular\tabular% save real tabular environment
\let\realEndTabular\endtabular% save end real tabular environment
\renewenvironment{tabular}[1]%
   {\renewcommand*{\textbf}[1]{\fb{##1}}\realTabular{#1}}
   {\realEndTabular}

\begin{document}
  \begin{tabular}{l}
    \textbf{a}a\\
    \textbf{a}a\\
    \textbf{a}a
  \end{tabular}

  \textbf{a}
\end{document}

The output is the same.

Btw, through instead of \renewcommand*{\textbf}[1]{\fb{##1}} you could use \let\textbf\fb

  • That's an elegant strategy; unfortunately in my case I want to redefine \textbf half way through a tabular environment and until its end. – user156626 Mar 11 '18 at 20:57

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.