2

This question already has an answer here:

I would like to use commands without having curly braces {} as delimiters. Mostly I would like that the commands work with a line-break as the delimiter.

Herewith a simple example, which does not work (My is displayed but missing the line)

\documentclass[12pt]{report}
\def\chap #1 {\chapter{#1}}
\def\sec #1 {\section{#1}}
\def\subsec #1 {\subsection{#1}}
\def\li #1 {\begin{itemize}\item #1\end{itemize}}
\begin{document}
\chap My First Chapter
\sec My First Section
\subsec My First Section
\li lkdsjfaljsfad
\li lkdsjfaljsfad
\li lkdsjfaljsfad
\end{document}

Would you know how to make it work?

marked as duplicate by user31729 Jun 25 '17 at 20:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Welcome to TeX.SE. You are aware about the role of {} in macro arguments? ;-) – user31729 Jun 25 '17 at 11:57
  • 4
    this just breaks all conventions of latex syntax, you really should not do it:-) but apart from the syntax don't you want a single list of three items, not three one-item lists? – David Carlisle Jun 25 '17 at 12:10
  • not much. Hopefully someone may help me. the above example almost works... but missed the >=2 words until the end of the line (LF) – debian82933 Jun 25 '17 at 12:11
  • Not sure what your actual goal is, but maybe the markdown package could be interesting for you – user36296 Jun 25 '17 at 12:33
  • 1
    @debian82933 Usually newbies ask complicated questions for issues, which are easy to solve. So what is your goal? – Keks Dose Jun 25 '17 at 12:38
4

Here's a LuaLaTeX-based solution. It sets up a Lua function that operates at a very early stage of processing: it scans all lines of input and simply replaces instances of \chap ..... with \chapter{.....}. (In Lua's string.gsub function, the $ character serves as the end-of-line marker.) Ditto for \sec ..., \subsec ..., and \li .... With this setup, LaTeX only gets to "see" the standard macros with standard argument formation; no need to define macros called \chap, \sec, etc.

enter image description here

% !TeX program = lualatex
\documentclass[12pt]{report}
\usepackage{luacode}
\begin{luacode}
function scan_ahead ( s )
  s = string.gsub ( s , "\\chap%s+(.*)$" , "\\chapter{%1}" )
  s = string.gsub ( s , "\\sec%s+(.*)$" , "\\section{%1}" )
  s = string.gsub ( s , "\\subsec%s+(.*)$" , "\\subsection{%1}" )
  s = string.gsub ( s , "\\li%s+(.*)$" , "\\begin{itemize}\\item %1\\end{itemize}")
  return s
end
luatexbase.add_to_callback ( "process_input_buffer",  scan_ahead , "scan_ahead" )
\end{luacode}

\begin{document}
\chap My First Chapter
\sec My First Section
\li lkdsjfaljsfad
\li abc def ghi
\subsec My First Section
\li jkl mno pqr stu
\end{document}
  • I don't use LUATEX, I have only pdflatex on my system. I cant lua – debian82933 Jun 25 '17 at 12:48
  • 1
    @debian82933 - What kind of TeX distribution do you have? Which software do you use for editing (and, presumably, compiling) your tex files? – Mico Jun 25 '17 at 12:49
  • Debian with Texlive (small installation), with latex, pdflatex, pdftex and bibtex. – debian82933 Jun 25 '17 at 13:04
  • 1
    @debian82933 - Any particular reason for not installing LuaTeX and LuaLaTeX as well? – Mico Jun 25 '17 at 13:12
3

I don't recommend this -- it does not look well and breaks the tradition of LaTeX commands.

The code is taken from egreg's answer at Using end-of-line delimiter in plain Tex macro (upvoted of course ;-))

There's actually nothing to gain from this and distracts from (proof)reading codes.

Especially \li is not really useful short version \item anyway.

The endline character has usually the catcode 5, so this is converted to a space or \par token (depending on the state) first before further processing the input. In order to make TeX recognize the end of line the catcode has to be changed, to 12 ('other'), in a group, so use

\def\chap{\begingroup\catcode`\^^M=12 \xchap}
{\catcode`\^^M=12 %
 \gdef\xchap#1^^M{\chapter{#1}\endgroup}%
}

to read the content until the end of the line and let the temporary macro \xchap to the display of \chapter{...} etc. (the same for \sec and \subsec.)

Of course, such approaches will break \chap[foo]{foo bar}, i.e. the short version of the chapter title meant for ToC and page headings are not available any longer.

\documentclass[12pt]{report}


\def\chap{\begingroup\catcode`\^^M=12 \xchap}
{\catcode`\^^M=12 %
 \gdef\xchap#1^^M{\chapter{#1}\endgroup}%
}


\def\sec{\begingroup\catcode`\^^M=12 \xsec}
{\catcode`\^^M=12 %
 \gdef\xsec#1^^M{\section{#1}\endgroup}%
}

\def\subsec{\begingroup\catcode`\^^M=12 \xsubsec}
{\catcode`\^^M=12 %
 \gdef\xsubsec#1^^M{\subsection{#1}\endgroup}%
}



\begin{document}
\chap My First Chapter
\sec My First Section
\subsec My First Section
\begin{itemize}
\item lkdsjfaljsfad
\item lkdsjfaljsfad
\item lkdsjfaljsfad
\end{itemize}
\end{document}

However, this is code golfing, in my point of view.

  • 1
    @debian82933: No, it isn't. It's pure laziness. In the long end it will bite you – user31729 Jun 25 '17 at 12:56
  • 1
    @debian82933: comparison of C and LaTeX lead to the wrong end... – user31729 Jun 25 '17 at 13:07
  • 1
    @debian82933: I'm using LaTeX for 20 years now and never complained about using those braces. Just because the syntax of macros is changeable to some extent this does not mean that it is advisable to do so. – user31729 Jun 25 '17 at 13:15
  • 1
    Use a US - keyboard layout and you don't need ALTGR ;-) – user31729 Jun 25 '17 at 13:28
  • 1
    @debian82933: Since you're using Debian you may use a keyboard layout switching command as well -- I have a regular German hardware keyboard, but most times in US software layout to type LaTeX. – user31729 Jun 25 '17 at 13:33

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