TeXShop is a really nice editor, especially since new commands can be added using macros. These macros can contain AppleScript, which makes them very flexible and powerful.
I like formating my headings in order to easily see sections, subsections etc. For that reason, I have added some macros creating sections etc. like this:
Macro "section":
% ==============================================================================
\section{#SEL##INS#}
\label{sec:#SEL#}
% ==============================================================================
My latex code then looks like that:
% ******************************************************************************
\chapter{Chapter}
\label{chapter:Chapter}
% ******************************************************************************
% ==============================================================================
\section{Section}
\label{sec:Section}
% ==============================================================================
% ------------------------------------------------------------------------------
\subsection{SubSection}
\label{sec:SubSection}
% ------------------------------------------------------------------------------
% ..............................................................................
\subsubsection{SubSubSection}
\label{sec:SubSubSection}
% ..............................................................................
\paragraph{Paragraph}
This works pretty well. But it becomes very painful to change a section to a subsection, because I do not only want to change "section" to "subsection", but the comment lines as well. So I added two macros including AppleScript in order to decrease or increase the level of a section.
I assume other people might find these macros useful, so I publish them here. They are written very quick and dirty, so you may have to adapt them to suit your preferences:
Macro: "Decrease Section Level":
--Applescript direct
-- Decrease Section Level
--Select Section Block including Comments to decrease section level, i.e. section become subsection, chapter becomes section and so on
-- (C) 2009 Jens von Pilgrim
--THE SCRIPT:
property texapp : "TeXShop"
tell application texapp
if texapp = "TeXShop" then
tell application "TeXShop" to set section to the content of the selection of the front document
else if texapp = "iTeXMac" then
--tell application "iTeXMac" to set section to (the selection of the text of the front document)
end if
set new_section to ""
repeat with ii from 1 to the count of the paragraphs of section
set this_line to paragraph ii of section
set new_line to this_line
set add to "true"
-- replace commands
if this_line contains "\\chapter" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\\\chapter/\\\\section/'"
end if
if this_line contains "\\section" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\\\section/\\\\subsection/'"
end if
if this_line contains "\\subsection" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\\\subsection/\\\\subsubsection/'"
end if
if this_line contains "\\subsubsection" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\\\subsubsection/\\\\paragraph/'"
end if
-- replace the comments
if this_line contains "% ******************************************************************************" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\*/=/g'"
end if
if this_line contains "% ==============================================================================" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/=/-/g'"
end if
if this_line contains "% ------------------------------------------------------------------------------" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/-/\\./g'"
end if
if this_line contains "% .............................................................................." then
set add to "false"
end if
if add="true" then
if new_section = "" then
set new_section to new_line
else
set new_section to new_section & return & new_line
end if
end if
end repeat
if texapp = "TeXShop" then
tell application "TeXShop" to set the selection of the front document to new_section
else if texapp = "iTeXMac" then
--tell application "iTeXMac" to insert new_section in the text of the front document
end if
end tell
Macro: "Increase Section Level":
--Applescript direct
-- Increase Section Level
--Select Section Block including Comments to decrease section level, i.e. section become subsection, chapter becomes section and so on
-- (C) 2009 Jens von Pilgrim
--THE SCRIPT:
property texapp : "TeXShop"
tell application texapp
if texapp = "TeXShop" then
tell application "TeXShop" to set section to the content of the selection of the front document
else if texapp = "iTeXMac" then
--tell application "iTeXMac" to set section to (the selection of the text of the front document)
end if
set new_section to ""
repeat with ii from 1 to the count of the paragraphs of section
set this_line to paragraph ii of section
set new_line to this_line
set add to "true"
-- replace commands
if this_line contains "\\section" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\\\section/\\\\chapter/'"
end if
if this_line contains "\\subsection" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\\\subsection/\\\\section/'"
end if
if this_line contains "\\subsubsection" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\\\subsubsection/\\\\subsection/'"
end if
if this_line contains "\\paragraph" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\\\paragraph/\\\\subsubsection/'"
set new_line to "% .............................................................................." & return & new_line & return & "% .............................................................................."
end if
-- replace the comments
if this_line contains "% ==============================================================================" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/=/\\*/g'"
end if
if this_line contains "% ------------------------------------------------------------------------------" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/-/=/g'"
end if
if this_line contains "% .............................................................................." then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\./-/g'"
end if
if add="true" then
if new_section = "" then
set new_section to new_line
else
set new_section to new_section & return & new_line
end if
end if
end repeat
if texapp = "TeXShop" then
tell application "TeXShop" to set the selection of the front document to new_section
else if texapp = "iTeXMac" then
--tell application "iTeXMac" to insert new_section in the text of the front document
end if
end tell
These two macros increase or decrease the level of a section. You have to simply select a section (or several sections), and the levels of all chapters, sections, and paragraphs are increased or decreased respectively. Besides the commands, the comments are reformatted as well.
When increasing the levels, the following modifications are made:
- section -> chapter
- subsection -> section
- subsubsection -> subsection
- paragraph -> subsubsection
When decreasing the levels, the following modifications are made:
- chapter -> section
- section -> subsection
- subsection -> subsubsection
- subsubsection -> paragraph
Note that chapters are not increased any further, and paragraphs are not decreased.
TeXShop is a really nice editor, especially since new commands can be added using macros. These macros can contain AppleScript, which makes them very flexible and powerful.
I like formating my headings in order to easily see sections, subsections etc. For that reason, I have added some macros creating sections etc. like this:
Macro "section":
% ==============================================================================
\section{#SEL##INS#}
\label{sec:#SEL#}
% ==============================================================================
My latex code then looks like that:
% ******************************************************************************
\chapter{Chapter}
\label{chapter:Chapter}
% ******************************************************************************
% ==============================================================================
\section{Section}
\label{sec:Section}
% ==============================================================================
% ------------------------------------------------------------------------------
\subsection{SubSection}
\label{sec:SubSection}
% ------------------------------------------------------------------------------
% ..............................................................................
\subsubsection{SubSubSection}
\label{sec:SubSubSection}
% ..............................................................................
\paragraph{Paragraph}
This works pretty well. But it becomes very painful to change a section to a subsection, because I do not only want to change "section" to "subsection", but the comment lines as well. So I added two macros including AppleScript in order to decrease or increase the level of a section.
I assume other people might find these macros useful, so I publish them here. They are written very quick and dirty, so you may have to adapt them to suit your preferences:
Macro: "Decrease Section Level":
--Applescript direct
-- Decrease Section Level
--Select Section Block including Comments to decrease section level, i.e. section become subsection, chapter becomes section and so on
-- (C) 2009 Jens von Pilgrim
--THE SCRIPT:
property texapp : "TeXShop"
tell application texapp
if texapp = "TeXShop" then
tell application "TeXShop" to set section to the content of the selection of the front document
else if texapp = "iTeXMac" then
--tell application "iTeXMac" to set section to (the selection of the text of the front document)
end if
set new_section to ""
repeat with ii from 1 to the count of the paragraphs of section
set this_line to paragraph ii of section
set new_line to this_line
set add to "true"
-- replace commands
if this_line contains "\\chapter" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\\\chapter/\\\\section/'"
end if
if this_line contains "\\section" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\\\section/\\\\subsection/'"
end if
if this_line contains "\\subsection" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\\\subsection/\\\\subsubsection/'"
end if
if this_line contains "\\subsubsection" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\\\subsubsection/\\\\paragraph/'"
end if
-- replace the comments
if this_line contains "% ******************************************************************************" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\*/=/g'"
end if
if this_line contains "% ==============================================================================" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/=/-/g'"
end if
if this_line contains "% ------------------------------------------------------------------------------" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/-/\\./g'"
end if
if this_line contains "% .............................................................................." then
set add to "false"
end if
if add="true" then
if new_section = "" then
set new_section to new_line
else
set new_section to new_section & return & new_line
end if
end if
end repeat
if texapp = "TeXShop" then
tell application "TeXShop" to set the selection of the front document to new_section
else if texapp = "iTeXMac" then
--tell application "iTeXMac" to insert new_section in the text of the front document
end if
end tell
Macro: "Increase Section Level":
--Applescript direct
-- Increase Section Level
--Select Section Block including Comments to decrease section level, i.e. section become subsection, chapter becomes section and so on
-- (C) 2009 Jens von Pilgrim
--THE SCRIPT:
property texapp : "TeXShop"
tell application texapp
if texapp = "TeXShop" then
tell application "TeXShop" to set section to the content of the selection of the front document
else if texapp = "iTeXMac" then
--tell application "iTeXMac" to set section to (the selection of the text of the front document)
end if
set new_section to ""
repeat with ii from 1 to the count of the paragraphs of section
set this_line to paragraph ii of section
set new_line to this_line
set add to "true"
-- replace commands
if this_line contains "\\section" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\\\section/\\\\chapter/'"
end if
if this_line contains "\\subsection" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\\\subsection/\\\\section/'"
end if
if this_line contains "\\subsubsection" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\\\subsubsection/\\\\subsection/'"
end if
if this_line contains "\\paragraph" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\\\paragraph/\\\\subsubsection/'"
set new_line to "% .............................................................................." & return & new_line & return & "% .............................................................................."
end if
-- replace the comments
if this_line contains "% ==============================================================================" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/=/\\*/g'"
end if
if this_line contains "% ------------------------------------------------------------------------------" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/-/=/g'"
end if
if this_line contains "% .............................................................................." then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\./-/g'"
end if
if add="true" then
if new_section = "" then
set new_section to new_line
else
set new_section to new_section & return & new_line
end if
end if
end repeat
if texapp = "TeXShop" then
tell application "TeXShop" to set the selection of the front document to new_section
else if texapp = "iTeXMac" then
--tell application "iTeXMac" to insert new_section in the text of the front document
end if
end tell
These two macros increase or decrease the level of a section. You have to simply select a section (or several sections), and the levels of all chapters, sections, and paragraphs are increased or decreased respectively. Besides the commands, the comments are reformatted as well.
When increasing the levels, the following modifications are made:
- section -> chapter
- subsection -> section
- subsubsection -> subsection
- paragraph -> subsubsection
When decreasing the levels, the following modifications are made:
- chapter -> section
- section -> subsection
- subsection -> subsubsection
- subsubsection -> paragraph
Note that chapters are not increased any further, and paragraphs are not decreased.
Increase/Decrease Section Level with TeXShop Macros
2 comments:
Dear Jens,
thank you for these scripts - very useful!
Just one suggestion: Whenever you downgrade or upgrade a heading (say, from Chapter to Section), the header command gets successfully converted from \chapter to \section. However, the labelprefix remains the same, i.e. "ch:".
I have therefore tried to modify your script so that the labelprefixes are being downgraded/upgraded just like the commands are downgraded/upgraded. In other words, the change from \chapter to \section it matched by a change of the labelprefix from "ch:" to "sec:".
However, my (naïve?) attempts with the following lines of code failed:
if this_line contains "ch:" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\\ch:/\\\sec:/'"
end if
Would you have any ideas as to how to fix the labelprefixes?
Thanks!
Jonas
@Jonas:
There's a tiny bug in your statement: there are three backslashes in the sed line where it must be only two:
if this_line contains "ch:" then
set new_line to do shell script ¬
"echo " & the quoted form of this_line & ¬
" | sed 's/\\ch:/\\sec:/'"
end if
That should work. If you change the label, you have to pay attention of updating references to it as well, of course.
Post a Comment