Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6689543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:31:54+00:00 2026-05-26T05:31:54+00:00

Preamble I’m using git as a version control system for a paper that my

  • 0

Preamble

I’m using git as a version control system for a paper that my lab is writing, in LaTeX. There are several people collaborating.

I’m running into git being stubborn about how it merges. Let’s say two people have made single-word changes to a line, and then attempt to merge them. Though git diff –word-diff seems capable of SHOWING the difference between the branches word-by-word, git merge seems unable to perform the merge word-by-word, and instead requires a manual merge.

With a LaTeX document this is particularly annoying, as the common habit when writing LaTeX is to write a full paragraph per line and just let your text editor handle word wrapping when displaying for you. We are working around for now by adding a newline for each sentence, so that git can at least merge changes on different sentences within a paragraph. But it will still get confused about multiple changes within a sentence, and this makes the text no longer wrap nicely of course.

The Question

Is there a way to git merge two files “word by word” rather than “line by line”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:31:54+00:00Added an answer on May 26, 2026 at 5:31 am

    Here’s a solution in the same vein as sehe’s, with a few changes which hopefully will address your comments:

    • This solution considers merging by sentence rather than by word, as you had been doing by hand, only now, the user will see a single line per paragraph, but git will see paragraphs broken into sentences. This seems to be more logical because adding/removing a sentence from a paragraph may be compatible with other changes in the paragraph, but it is probably more desirable to have a manual merge when the same sentence is edited by two commits. This also has the benefit of the “clean” snapshots to still be somewhat human readable (and latex compilable!).
    • The filters are one-line commands, which should make it easier to port this to collaborators.

    As in saha’s solution make a (or append to) .gittatributes.

        *.tex filter=sentencebreak
    

    Now to implement the clean and smudge filters:

        git config filter.sentencebreak.clean "perl -pe \"s/[.]*?(\\?|\\!|\\.|'') /$&%NL%\\n/g unless m/%/||m/^[\\ *\\\\\\]/\""
        git config filter.sentencebreak.smudge "perl -pe \"s/%NL%\n//gm\""
    

    I’ve created a test file with the following contents, notice the one-line paragraph.

        \chapter{Tumbling Tumbleweeds. Intro}
        A way out west there was a fella, fella I want to tell you about, fella by the name of Jeff Lebowski.  At least, that was the handle his lovin' parents gave him, but he never had much use for it himself. This Lebowski, he called himself the Dude. Now, Dude, that's a name no one would self-apply where I come from.  But then, there was a lot about the Dude that didn't make a whole lot of sense to me.  And a lot about where he lived, like- wise.  But then again, maybe that's why I found the place s'durned innarestin'.
    
        This line has two sentences. But it also ends with a comment. % here
    

    After we commit it to the local repo, we can see the raw contents.

        $ git show HEAD:test.tex
    
        \chapter{Tumbling Tumbleweeds. Intro}
        A way out west there was a fella, fella I want to tell you about, fella by the name of Jeff Lebowski. %NL%
         At least, that was the handle his lovin' parents gave him, but he never had much use for it himself. %NL%
        This Lebowski, he called himself the Dude. %NL%
        Now, Dude, that's a name no one would self-apply where I come from. %NL%
         But then, there was a lot about the Dude that didn't make a whole lot of sense to me. %NL%
         And a lot about where he lived, like- wise. %NL%
         But then again, maybe that's why I found the place s'durned innarestin'.
    
        This line has two sentences. But it also ends with a comment. % here
    

    So the rules of the clean filter are whenever it finds a string of text that ends with . or ? or ! or '' (that’s the latex way to do double quotes) then a space, it will add %NL% and a newline character. But it ignores lines that start with \ (latex commands) or contain a comment anywhere (so that comments cannot become part of the main text).

    The smudge filter removes %NL% and the newline.

    Diffing and merging is done on the ‘clean’ files so changes to paragraphs are merged sentence by sentence. This is the desired behavior.

    The nice thing is that the latex file should compile in either the clean or smudged state, so there is some hope for collaborators to not need to do anything. Finally, you could put the git config commands in a shell script that is part of the repo so a collaborator would just have to run it in the root of the repo to get configured.

        #!/bin/bash
    
        git config filter.sentencebreak.clean "perl -pe \"s/[.]*?(\\?|\\!|\\.|'') /$&%NL%\\n/g unless m/%/||m/^[\\ *\\\\\\]/\""
        git config filter.sentencebreak.smudge "perl -pe \"s/%NL%\n//gm\""
    
        fileArray=($(find . -iname "*.tex"))
    
        for (( i=0; i<${#fileArray[@]}; i++ ));
        do
            perl -pe "s/%NL%\n//gm" < ${fileArray[$i]} > temp
            mv temp ${fileArray[$i]}
        done
    

    That last little bit is a hack because when this script is first run, the branch is already checked out (in the clean form) and it doesn’t get smudged automatically.

    You can add this script and the .gitattributes file to the repo, then new users just need to clone, then run the script in the root of the repo.

    I think this script even runs on windows git if done in git bash.

    Drawbacks:

    • This doesn’t handle lines with comments smartly, it just ignores them.
    • %NL% is kind of ugly
    • The filters may screw up some equations (not sure about this).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a really long table in LaTeX that spans several pages and is
Is there any single magic preamble that will make a Perl script run under
Preamble: I investigated how to version an API and found several ways to do
What sort of code should I put in preamble, such that every math fomula
I am using ntheorem to typeset a set of conditions. In my preamble I
PREAMBLE: the question is wildly obsolete. There's no more Google Checkout, no more Checkout
How can i create PDF thumbnails in TeXShop using LaTeX and thumbpdf? I have
Preamble: I'm working at heavy-loaded applicaion that produces large data arrays. I wrote the
Preamble Using VTK library with C++, quite often I have to write something like
Preamble: I'm creating some tables in SQL Server that will be accessed via Linq-to-entities

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.