Ed Markey and Jay Livingstone were elected Tuesday in a special election.

Ed Markey and Jay Livingstone were elected Tuesday in a special election.

In the Tuesday special election, Ed Markey took 55 percent of the statewide vote to become a U.S. senator, leaving behind his seat representing Massachusetts’ 5th District in the U.S. House of Representatives.

Because this was a special election, Markey, 66, will have 17 months in office before his next Election Day.

His Republican opponent, Gabriel E. Gomez, 47, a former Navy SEAL and private equity investor, got 45 percent of the vote statewide. Richard Heos, of the Twelve Visions Party, got 0.4 percent of the vote.

This breaks down to Markey getting 642,988 votes; Gomez getting 525,080; and Heos getting 4,518.

In Cambridge, the figures were a bit more lopsided in favor of Markey: He got 18,572 of the 20,900 ballots cast in the Senate race, or 89 percent of the vote, according to unofficial results from the city’s Election Commission. Gomez got 2,232 votes or 11 percent. Heos got 50 votes, or 0.24 percent of the ballots and there were 46 write-in votes.

The special election also chose a replacement for Martha “Marty” Walz in the 8th Suffolk Representative District as she leaves office to become president and chief executive at Planned Parenthood Massachusetts. The district includes Cambridgeport and parts of the Massachusetts Institute of Technology area as well as parts of Beacon Hill, Back Bay and the West End in Boston.

Jay Livingstone, who lives on Beacon Hill and has been working as an employment discrimination and criminal defense attorney and adjunct professor at Northeastern Law School, got 2,013 votes in Cambridge, or 97 percent (with the remaining 52 being write-ins), and 3,750 votes in Boston, or 93 percent (with the remaining 264 being write-ins), according to that city’s unofficial election results.