Skip to content

Instantly share code, notes, and snippets.

@flat35hd99
Created March 22, 2023 14:04
Show Gist options
  • Save flat35hd99/e7ea6630a6ef71b0adbfe72c2138e9be to your computer and use it in GitHub Desktop.
Save flat35hd99/e7ea6630a6ef71b0adbfe72c2138e9be to your computer and use it in GitHub Desktop.
Remove \centering of LaTeX output of Pandoc
-- This script avoid the centering of figures in LaTeX pdf by Pandoc.
-- This script can be used with pandoc-crossref.
-- You can use this script like:
-- pandoc -s --lua-filter uncentering-figures.lua -o output.pdf input.md
-- MIT License
-- Copyright (c) 2023 flat35hd99
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
function Image(elem)
local src = pandoc.utils.stringify(elem.src)
local alt = ''
for i, v in ipairs(elem.caption) do
if v == nil or v.text == nil then
goto continue
end
if v.t == 'Math' then
alt = alt .. ' \\(' .. v.text .. '\\)'
else
alt = alt .. ' ' .. v.text
end
::continue::
end
if elem.attr.identifier == '' then
return pandoc.RawInline('latex',
'\\begin{figure}\n'
.. '\\includegraphics{' .. src .. '}\n'
.. '\\caption{' .. alt .. '}\n'
.. '\\end{figure}\n'
)
end
return
pandoc.RawInline('latex',
'\\begin{figure}\n'
.. '\\hypertarget{' .. elem.attr.identifier .. '}{%\n'
.. '\\includegraphics{' .. src .. '}\n'
.. '\\caption{' .. alt .. '}\n\\label{' .. elem.attr.identifier .. '}\n'
.. '}\n'
.. '\\end{figure}\n'
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment